1

For a very simple XML like this:

<catalog>
    <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>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>

and a simple xslt:

<xsl:template match="/">
 <xsl:apply-templates/>  
</xsl:template>

why does it produce output like:

Empire Burlesque
Bob Dylan
USA
Columbia
10.90
1985

Hide your heart
Bonnie Tyler
UK
CBS Records
9.90
1988

Greatest Hits
Dolly Parton
USA
RCA
9.90
1982

Where have all the XML tags gone? Should I surround <xsl:apply-templates/> with <xsl:copy> tags to get that to work?

Wayne
  • 59,728
  • 15
  • 131
  • 126
Kevin
  • 6,711
  • 16
  • 60
  • 107
  • You probably should not " surround with `` with tags " -- anyone can show you what to do -- especially if you show us the wanted output and explain any desired rules/properties/constraints for the transformation. – Dimitre Novatchev Feb 23 '11 at 17:49
  • First duplicate found [what is the reason behind XSLT parser's this behavior ?](http://stackoverflow.com/questions/3360017/what-is-the-reason-behind-xslt-parsers-this-behavior) –  Feb 23 '11 at 18:14

1 Answers1

5

It's because of the built-in templates, which visit all elements and print the values of text and attribute nodes (as long as a template applies to them). See my answer to this previous question for a full explanation:

XSLT 1.0 text nodes printing by default

You can use xsl:copy to perform the identity transform:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Which will output a copy of your source document.

Community
  • 1
  • 1
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • Code provided by OP doesn't "print attribute nodes". Either does empty XSL file. They are printed if they are applied. [Proof link](http://www.w3.org/TR/xslt#built-in-rule) – Flack Feb 23 '11 at 19:24
  • @Flack - Of course you're right, but that's not the built-in template's fault. It's just because OP's XSLT never visits any attribute nodes. I thought about clarifying that. Maybe I will now. – Wayne Feb 23 '11 at 20:27