0

I have been trying to transform an XML file using XSLT but due to some problem, namely "xmlns:ns0" and ns0:catalog, It is not transforming. Please help me to resolve.

The XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:catalog xmlns:ns0="http://sap.com">
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</ns0:catalog>

The XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <MyCatalog>
            <xsl:for-each select="catalog/cd">
                <cd>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </cd>
            </xsl:for-each>
        </MyCatalog>
    </xsl:template>
</xsl:stylesheet>

Expected Results:

<?xml version="1.0" encoding="UTF-8"?>
<MyCatalog>
    <cd>
        <title>Empire Burlesque</title>
    </cd>
    <cd>
        <title>Hide your heart</title>
    </cd>
</MyCatalog>
Ranji
  • 1

1 Answers1

0

You need to add the namespace to your selector:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://sap.com">

    <xsl:template match="/">
        <MyCatalog>
            <xsl:for-each select="ns0:catalog/cd">
                <cd>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </cd>
            </xsl:for-each>
        </MyCatalog>
    </xsl:template>
</xsl:stylesheet>
Charles Mager
  • 25,735
  • 2
  • 35
  • 45