Multi level and multiple namespace in XML using XSLT

风流意气都作罢 提交于 2020-01-17 07:27:22

问题


I am very new in XSLT. I need to crate a XML like this.

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body>
        <GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService">
          <GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

I am able to produce the xml upto this lavel. but as there is multi lavel namespace is used I got confused.

  <?xml version="1.0" encoding="UTF-8"?>
        <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
            <s:Body>
                <GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService"/>
            </s:Body>
        </s:Envelope>

I am using this XSLT for the same

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
      version="2.0">
    <xsl:output indent="yes"/>
     <xsl:template match = "/" > 
         <s:Envelope > 
           <s:Body>
            <xsl:element name = "GetRecordResponse" namespace = "urn:RedIron.RetailRepository.Services.SearchService" > 
            </xsl:element> 
            </s:Body>
          </s:Envelope>             
       </xsl:template> 
    </xsl:stylesheet>

回答1:


If you want to add these namespace nodes, you can use xsl:namespace. This is only available in processors supporting XSLT2.0 though.

<xsl:element name="GetRecordResponse" namespace="urn:RedIron.RetailRepository.Services.SearchService"> 
    <xsl:element name="GetRecordResult" namespace="urn:RedIron.RetailRepository.Services.SearchService">
        <xsl:namespace name="a" select="'urn:RedIron.RetailRepository.Core'" />
        <xsl:namespace name="i" select="'http://www.w3.org/2001/XMLSchema-instance'" />
    </xsl:element>
</xsl:element> 

Alternatively, you can probably just write out the element names and there namespace declarations literally

<GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService" > 
    <GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</GetRecordResponse> 

Note that if you move the creation of GetRecordResult to a different template, for example, you might need to specify the default namespace on it, as it will no longer inherit the default namespce from GetRecordResponse

 <GetRecordResult xmlns="urn:RedIron.RetailRepository.Services.SearchService"  
    xmlns:a="urn:RedIron.RetailRepository.Core" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />


来源:https://stackoverflow.com/questions/41483583/multi-level-and-multiple-namespace-in-xml-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!