gino gino
gino gino

Reputation: 269

XSL and XPATH issue match

I have this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cd.xsl"?>
    <produce>
        <item>apple<x>kk</x><y>jj</y></item>
        <item>banana<x>aaa</x></item>
        <item>pepper<x>qqq</x></item>
    </produce>

and this XLS file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
  <xsl:output method="html"/>

  <xsl:template match="/">
     <html>
    <body>
    <ul>
    <li><xsl:value-of select="node()"/></li>
    </ul>
    </body>
    </html>
  </xsl:template>
  </xsl:stylesheet>

I don't understand the difference between "/" and "/*" match and for this reason I'm doing various tests, for example like those above I get this:

type="text/xsl" href="cd.xsl"

and I don't understand why.(I expected produce tag).

however if I use this XLS file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    version="1.0">
      <xsl:output method="html"/>

      <xsl:template match="/*">
         <html>
        <body>
        <ul>
        <li><xsl:value-of select="node()"/></li>
        </ul>
        </body>
        </html>
      </xsl:template>
      </xsl:stylesheet>

I get empty page(only the black point of the tag li). Can you explain me those difference?

Upvotes: 2

Views: 62

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22647

If you want to understand what an XSL transformation does, use a tool that will show you the actual resulting code (e.g. xsltransform.net) - not a browser. – michael.hor257k

I very much agree with this, use e.g. http://xsltransform.net/.


TL;DR: / is the document node, /* is the outermost element of your input document

The output you get from the first stylesheet is

<html>
   <body>
      <ul>
         <li>type="text/xsl" href="cd.xsl"</li>
      </ul>
   </body>
</html>

and I can see why that is surprising to you. Let's look at the XSLT code, where you match the document node:

<xsl:template match="/">

In this template, the only reference you are making to the input document is

<xsl:value-of select="node()"/>

which selects the value of the first node that is a child of the document node (/). Actually, node() is a set of nodes, but in XSLT 1.0, in contexts like these only the first item in such a set is used.

So, what is a node? In an XML document model (e.g. XDM) there are many different types of nodes, among others: element nodes, attribute nodes, processing instructions. Now have a look at your input document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cd.xsl"?>
<produce>
    <item>apple<x>kk</x><y>jj</y></item>
    <item>banana<x>aaa</x></item>
    <item>pepper<x>qqq</x></item>
</produce>

The first line is an XML declaration that is not part of the document. The second line is a processing instruction that tells your browser where to look for the XSLT stylesheet. A processing instruction is part of the document.

This processing instruction is the first child of the document node - which is why you get its content as the result of

<xsl:value-of select="node()"/>

From all this it follows that if you remove the processing instruction from the input document, the output will look like

<html>
   <body>
      <ul>
         <li>
            applekkjj
                    bananaaaa
                    pepperqqq

         </li>
      </ul>
   </body>
</html>

because now, the produce element is the first child node of /. What you see as the result is the string value of the produce element.


On the other hand, if the template matches /*, then the output is not an empty page, it is

<html>
   <body>
      <ul>
         <li>

         </li>
      </ul>
   </body>
</html>

/* means the outermost element of your XML document, in this case produce. Now, the produce element is the context for the xsl:value-of:

<xsl:value-of select="node()"/>

Again, this will select the first child node of the produce element - which is a text node that only contains whitespace.

Upvotes: 4

Related Questions