Reputation: 11
I am facing some issue in my xslt transformation. I want to remove extra namespace added in my result during xslt transformation.
here is the my source xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<Root xmlns="http://www.abc123.org/xyz/ase" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
<BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
<ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
<Success>false</Success>
</Header>
<Data>
<ErrorMessage>
<ID/>
<Type>Invalid Data</Type>
<Description>InvalidData</Description>
</ErrorMessage>
</Data>
</Root>
</soapenv:Body>
</soapenv:Envelope>
here is my xslt code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes="soapenv">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:*">
<xsl:apply-templates select="@* | node()" />
</xsl:template>
</xsl:stylesheet>
Expected Output:
<Root xmlns="http://www.abc123.org/xyz/ase"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
<BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
<ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
<Success>false</Success>
</Header>
<Data>
<ErrorMessage>
<ID/>
<Type>Invalid Data</Type>
<Description>InvalidData</Description>
</ErrorMessage>
</Data>
</Root>
Actual Output:
<Root xmlns="http://www.abc123.org/xyz/ase"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<Header>
<CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
<BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
<ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
<Success>false</Success>
</Header>
<Data>
<ErrorMessage>
<ID/>
<Type>Invalid Data</Type>
<Description>InvalidData</Description>
</ErrorMessage>
</Data>
</Root>
Here, I am getting extra namespace "xmlns:soapenv" that I need to remove.
Kindly suggest correction.
Thanks
Upvotes: 1
Views: 346
Reputation: 70638
xsl:copy
will copy any namespaces declared in the XML, even if the element itself does not use the namespace.
What you can do, is instead of using xsl:copy
on elements, use xsl:element
to create a new element, and copy across only the namespace declarations you need....
Try adding this template to your XSLT
<xsl:template match="*[namespace-uri() != 'http://schemas.xmlsoap.org/soap/envelope/']" priority="2">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="namespace::*[. != 'http://schemas.xmlsoap.org/soap/envelope/']" />
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
Upvotes: 2