0

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
Sdey
  • 5
  • 3
  • 1
    please do read [this](http://stackoverflow.com/help/mcve) and edit your question due to it. – uL1 Sep 21 '16 at 05:18
  • Namespace are to be **used**, not *avoided*. See here how: http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Sep 21 '16 at 05:25
  • Thanks Michael, but do not fetching the value..Can you please explain? – Sdey Sep 21 '16 at 05:48
  • Every child and self of `ListResponse` is not anymore under the namespace `http://schemas.xmlsoap.org/soap/envelope/`[prefix `S`]. The namespace changes to `urn:abcde:xyz:1`. Simply, you need a prefix for that and use the new prefix in your statement on the correct nodes. – uL1 Sep 21 '16 at 05:57
  • XSLT code.. – Sdey Sep 21 '16 at 05:58
  • @Sdey Please don't post code in comments. Edit your question and add your XSLT there. – michael.hor257k Sep 21 '16 at 05:59
  • I'm sorry but editor do not allow me to edit my new code as suggested by uL1 – Sdey Sep 21 '16 at 06:09

2 Answers2

1

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

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.

Community
  • 1
  • 1
uL1
  • 2,117
  • 2
  • 17
  • 28