XSLT to change namespace in element

北城以北 提交于 2019-12-01 14:04:38

The below XSLT will help you to get the desired results:

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.xx.com"
    xmlns:ns="http://www.mnv.com/elc/sap"
    exclude-result-prefixes="ns"> 
    <xsl:output encoding='UTF-8' indent='yes' method='xml'/>

    <!-- copy everything into the output -->
    <xsl:template match='@*|node()'>
        <xsl:copy>
            <xsl:apply-templates select='@*, node()'/>
        </xsl:copy>
    </xsl:template>

    <!-- template to match ns:IRenvelope element and creating a new element -->
    <xsl:template match="ns:IRenvelope">
        <xsl:element name="IRL" namespace="http://www.xx.com">
            <xsl:apply-templates select="@*, node()"/>
        </xsl:element>
    </xsl:template>

    <!-- template to change the namespace 
         of the elements  
         from "http://www.mnv.com/elc/sap" 
         to "http://www.xx.com" -->
    <xsl:template match="ns:*">
        <xsl:element name="{local-name()}" namespace="http://www.xx.com">
            <xsl:apply-templates select="@*, node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Here, the last two templates match the ns:IRenvelope and all the elements with namespace http://www.mnv.com/elc/sap, respectively. Using the xsl:element and its namespace attribute, we can create the new elements with desired namespace.

You can also declare the desired namespaces with prefixes and create elements as below:

<xsd:IRL xmlns:xsd="http://www.xx.com">
    ...
</xsd:IRL>

For XSLT-1.0:

Just replace the ,(comma) to use a |(pipe) in apply-templates as using comma to sequence the action is supported in 2.0:

<xsl:apply-templates select="@* | node()"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!