Reputation: 125
Original post:
XSLT to change namespace in element
Its regarding my earlier post with xslt program to replace a namespace. I had the question answered but when testing with my system it shows error as the application system i work support version 1.0
Please Need some help to make the code compatibly with version 1.0. Below is the xsl:
<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>
my xml message would be:
<GMessage xmlns="http://www.giffgaff.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<body>
<IRenvelope xmlns="http://www.mnv.com/elc/sap">
<Keys>
<Key Type="TaxOfficeNumber">635</Key>
</Keys>
</IRenvelope>
</body>
</GMessage>
expected result:
<?xml version="1.0" encoding="UTF-8"?>
<GMessage xmlns="http://www.giffgaff.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<body>
<IRL xmlns="http://www.xx.com">
<Keys>
<Key Type="TaxOfficeNumber">635</Key>
</Keys>
</IRL>
</body>
</GMessage>
Upvotes: 0
Views: 979
Reputation: 70618
The problem lies solely with these lines...
<xsl:apply-templates select='@*, node()'/>
This syntax is not valid in XSLT 1.0. In XSLT 2.0, the comma is used to build a "sequence".
However, you can simply replace occurrences of that line with this instead, which will work in both XSLT 1.0 and 2.0
<xsl:apply-templates select='@*|node()'/>
The pipe character is a union operator to join node sets.
Upvotes: 1