user4800620
user4800620

Reputation:

XSL can't use template match

I try for first time xsl, and in 3 hour can't print a single line!

btw. MOST simple xml file:

 <?xml version="1.0" encoding="UTF-8"?>

<book>
    <author>qqqqqq</author>
    <title>Srwrtwt</title>
    <title>yoo</title>
    <price>$10.00</price>
</book>

And one version of xsl:

<?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"/>

    <xsl:template match="author">
        <html>

        </html>
    </xsl:template>

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

</xsl:stylesheet>

The output is:

    <html></html>
    <h1>Srwrtwt</h1>
    <h1>yoo</h1>
    $10.00

And I'm ok with that, BUT if i change the xsl like this:

<?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"/>

    <xsl:template match="/">
        <html>

        </html>
    </xsl:template>

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

</xsl:stylesheet>

This is the output, why template match="title" NOW does not work?:

<html></html>

The fact is that I have done similar example to try to understand why this happens and in other cases it works: example:

<?xml version="1.0"?>
<page>
    <title>Hello World</title>
    <content>
        <x>dewjnf</x>
        <paragraph>
            <resume>This is my first page!</resume>
        </paragraph>
    </content>
</page> 

the xsl:

<?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" />
    <xsl:template match="/">
        <html>
            <head>
                <title>
                    <xsl:value-of select="page/title" />
                </title>
                <style>
                    h1{
                    color: blue;
                    font-style: italic;
                    }
                </style>
            </head>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="title">
        <div align="center">
            <h1>
                <xsl:value-of select="." />
            </h1>
        </div>
    </xsl:template>
    <xsl:template match="resume">
        <p>
            <xsl:apply-templates />
        </p>
    </xsl:template>
</xsl:stylesheet>

And the output is exactly what I expected with the template match working: To me seems exactly the same thing of the previous example but with different behave.

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
<style>
h1 {
    color: blue;
    font-style: italic;
}
</style>
</head>
<body>

    <div align="center">
        <h1>Hello World</h1>
    </div>

    dewjnf

    <p>This is my first page!</p>



</body>
</html>

The question is why sometimes when there is match="/" the others matches does not work and sometimes yes? where is the error?

Upvotes: 0

Views: 305

Answers (2)

Stefan Hegny
Stefan Hegny

Reputation: 2187

Why does the title template not work in the second example? Because it is not reached. You should do an <xsl:apply-templates/> inside the <xsl:template match="/"/><html>... - it won't recurse by itself

Upvotes: 1

hr_117
hr_117

Reputation: 9627

If you like to have more matches you need to add

<xsl:apply-templates />

in your templates. If you don't the child nodes will not considered.

Upvotes: 0

Related Questions