问题
I have a input xml like..:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ListResponse xmlns="urn:abcde:xyz:1">
<Gtins>
<Gtin>
<gtinID>11111</gtinID>
<name>222222</name>
<label>S11111 - EA</label>
<description>XYZ</description>
<value>11111</value>
</Gtin>
<Gtin>
<gtinID>999999</gtinID>
<name>999999</name>
<label>asdfg</label>
<description>ghgj</description>
<value>999999</value>
</Gtin>
</Gtins>
</ListResponse>
</S:Body>
</S:Envelope>
How do I select each and every value of the node 'Gtin' and avoid namespace through XSLT?
Output XML should be...
<ns0:RFC xmlns:ns0="http://asd.com">
<Gtins>
<Gtin>
<gtinID>11111</gtinID>
<name>222222</name>
<label>S11111 - EA</label>
<description>XYZ</description>
<value>11111</value>
</Gtin>
<Gtin>
<gtinID>999999</gtinID>
<name>999999</name>
<label>asdfg</label>
<description>ghgj</description>
<value>999999</value>
</Gtin>
</Gtins>
</ns0:RFC>
回答1:
Conclusion / Summary of comments and other already answered questions.
In advice of @michael.hor257k please visit this thread. Accepted answer to the same problem as you have. Use of namespace
The problem: your XML puts its elements in a namespace.
Solution: declare the same namespace in your stylesheet, assign it a prefix and use that prefix to address the elements in the source XML.
Due to your given input, start and continue on your own with this base:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:abc="urn:abcde:xyz:1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/S:Envelope">
<!-- select any child - exemplary -->
<xsl:value-of select="S:Body/abc:ListResponse/abc:Gtins/abc:Gtin/abc:gtinID" />
</xsl:template>
</xsl:stylesheet>
Furthermore:
This is an equivalent version of your input-xml:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="urn:abcde:xyz:1">
<S:Body>
<abc:ListResponse>
<abc:Gtins>
<abc:Gtin>
<abc:gtinID>11111</abc:gtinID>
<abc:name>222222</abc:name>
<abc:label>S11111 - EA</abc:label>
<abc:description>XYZ</abc:description>
<abc:value>11111</abc:value>
</abc:Gtin>
<abc:Gtin>
<abc:gtinID>999999</abc:gtinID>
<abc:name>999999</abc:name>
<abc:label>asdfg</abc:label>
<abc:description>ghgj</abc:description>
<abc:value>999999</abc:value>
</abc:Gtin>
</abc:Gtins>
</abc:ListResponse>
</S:Body>
</S:Envelope>
Namespaces are there for reasons! Use them and don't get rid of them just for "i don't understand them, so i want to remove them"-reasons. Don't get me wrong, your result-xml CAN be without namespaces, no doubt about that. There are many examples on SO, go for it.
回答2:
Try this as your starting point:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xyz="urn:abcde:xyz:1"
exclude-result-prefixes="S xyz">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/S:Envelope">
<ns0:RFC xmlns:ns0="http://asd.com">
<Gtins>
<xsl:for-each select="S:Body/xyz:ListResponse/xyz:Gtins/xyz:Gtin">
<Gtin>
<gtinID>
<xsl:value-of select="xyz:GtinID"/>
</gtinID>
<!-- more here -->
</Gtin>
</xsl:for-each>
</Gtins>
</ns0:RFC>
</xsl:template>
</xsl:stylesheet>
Or even simpler:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xyz="urn:abcde:xyz:1"
exclude-result-prefixes="S xyz">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/S:Envelope">
<ns0:RFC xmlns:ns0="http://asd.com">
<Gtins>
<xsl:apply-templates select="S:Body/xyz:ListResponse/xyz:Gtins/xyz:Gtin"/>
</Gtins>
</ns0:RFC>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/39607996/xslt-to-avoid-namespace