0
votes

I'm trying to understand the basic idea how XSLT 1.0 works. I'm using Firefox browser for learning. Following example is not practical in any way but rather illustrates base for my question.

For source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="helloworld.xsl"?>

<messages>
    <greeting>Hello World!</greeting>
    <farewell>Bye World!</farewell>
</messages>

And stylesheet:

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

        <xsl:template match="greeting">
            <b><xsl:value-of select="." /></b>
        </xsl:template>

        <xsl:template match="farewell">
            <b><xsl:value-of select="." /></b>
        </xsl:template>

</xsl:stylesheet>

I get "Hello Word!" and "Bye World!" rendered bold. If i use following stylesheet:

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

            <xsl:template match="greeting">
                <b><xsl:value-of select="." /></b>
            </xsl:template>

            <xsl:template match="greeting">
                <i><xsl:value-of select="." /></i>
            </xsl:template>

    </xsl:stylesheet>

Then "Bye World!" as expected is not bold, but "Hello World" also is not bold, only in italic. What happened? In first version of stylesheet it seems that both templates where executed - both matched document node. In second version seems like only second was executed or it's output has overwritten the first one. I would be very grateful for explanation on what basis XSLT processor matches nodes with templates in both stylesheets.

From what I understand: at the beginning of transformation document node is selected as context. XSLT processor iterates through nodes in context and tries to find template for each node to match and execute. Context is the synonym for "current node". What I don't understand is: does processor iterate through nodes being children of current node (so in my example only message node, as it is an only child of document node) or through all nodes being descendant of current node?

1
"I'm trying to understand the basic idea how XSLT 1.0 works. I'm using Firefox browser for learning": why not start reading a book (e.g. cranesoftwrights.github.io/books/ptux/index.htm) that explains it or look at the public spec w3.org/TR/xslt-10/#section-Processing-Model, w3.org/TR/xslt-10/#built-in-rule, w3.org/TR/xslt-10/#section-Applying-Template-Rules? - Martin Honnen
To be honest official documentation from W3 put me off with using term "current nodes" without defining it, but certainly I should read it more. As for PTUX, thank you because I didn't know about it and it looks promising. - Tomczi
Your second set matches on greeting twice which is likely an error actually. - Kevin Brown
There are lots of tutorials and books around that try and explain it, with different levels of formality designed for different audiences. You can't expect a personal tutorial on StackOverflow, and if you get one there's no reason why it should be any better than the many others out there. Remember that online resources are often written by one person in a couple of hours and not reviewed by anyone except the author, whereas published books are written over a period of months and very carefully reviewed by lots of people both for technical accuracy and for readability. - Michael Kay

1 Answers

1
votes

To understand what happened, you should look at the HTML code produced as a result of your XSL transformation - not at how this code is rendered on screen. You get:

<i>Hello World!</i>
Bye World!

The first line is produced by the (last) template matching greeting*. The second line is produced by the built-in template rules that handle the farewell element in the absence of a matching template. These templates copy any text nodes to the output.


(*) It is an error to have two conflicting templates. However, the specification permits the processor to ignore the error and apply the last matching template in the XSLT document.