0

I got a sample xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<collection> 
    <cd>
        <title>Boys for Pele</title>
        <artist>Tori Amos</artist>
        <tracks>
            <track type="vocal">
                <name>Horses</name>
                <length>3.5</length>
            </track>
            <track type="instrumental">
                <name>Blood roses</name>
                <length>3.2</length>
            </track>
            <track type="vocal">
                <name>Father lucifer</name>
                <length>3.8</length>
            </track>
            <track type="instrumental">
                <name>Professional widow</name>
                <length>4.1</length>
            </track>
            <track type="vocal">
                <name>Mr. Zebra</name>
                <length>3.6</length>
            </track>
        </tracks>
    </cd> 
</collection>

and I use a sample xsl for testing :

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

The question is the '<xsl:template match="/"/>' code. When I test it, nothing comes out When I replace it with '<xsl:template match="collection/cd"/> , still nothing comes out But when I replace it with '<xsl:template match="collection/cd/tracks"/>' , the following result appear:-

Boys for Pele Tori Amos The Ghosts that Hunt me Crash TestDummies

But this value is not located inside the element of the xml, out outside it.

Can anyone offer some help on that? thanks very much!

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Max Ng
  • 25
  • 1
  • 5

1 Answers1

0

Your empty template is overriding the default template, which by default copies all text. The action of your template is nothing, so anything that it matches is omitted, and anything it doesn't match falls back to the default.

match="/"

The processing engine reaches your template for the root, and applies it, resulting in empty output.

match="collection/cd"

The processing engine reaches your template after having copied all the (lack of) text in the node, resulting in empty output.

match="collection/cd/tracks"

The processing engine reaches your template after having copied all text in the <title> and <artist> tags, and then applies the "do nothing" rule to <tracks>, resulting in the title-text artist-text title-text artist-text ... result.

Caleth
  • 52,200
  • 2
  • 44
  • 75