Xslt : Create a namespace dynamically

后端 未结 1 1866
Happy的楠姐
Happy的楠姐 2020-12-11 13:56

Following are the nodes in XML Data

\"http://webser.part.site\"
nida


        
相关标签:
1条回答
  • 2020-12-11 14:39

    I. This XSLT 2.0 transformation:

    <xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:param name="pUrl" select="'some: namespace'"/>
    
     <xsl:template match="/*">
         <xsl:element name="{name()}"
              namespace="http://schemas.xmlsoap.org/soap/envelope/">
          <xsl:sequence select="namespace::*[not(name()='web')]"/>
          <xsl:namespace name="web" select="$pUrl"/>
         </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:web="http://webser.part.site"/>
    

    produces the wanted, correct result (the 'web' namespace produced from the value of a parameter):

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:web="some: namespace"/>
    

    II. This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:param name="pUrl" select="'some: namespace'"/>
    
        <xsl:variable name="vrtfDummy">
         <xsl:element name="web:dummy" namespace="{$pUrl}"/>
        </xsl:variable>
    
        <xsl:variable name="vNS" select="ext:node-set($vrtfDummy)/*/namespace::web"/>
    
     <xsl:template match="/*">
         <xsl:element name="{name()}"
              namespace="http://schemas.xmlsoap.org/soap/envelope/">
          <xsl:copy-of select="namespace::*[not(name()='web')]"/>
          <xsl:copy-of select="$vNS"/>
         </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the same XML document (above), again produces the wanted, correct result:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="some: namespace"/>
    
    0 讨论(0)
提交回复
热议问题