Ikke
Ikke

Reputation: 101231

xslt apply-templates selects all remaining textnodes

I have this simplified xml:

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

<a>
    <b>
        <c>
            <d>1</d>
            <e>2</e>
        </c>
    </b>
    <f>
        <g>3</g>
    </f>
</a>

This is the xslt i try to apply:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

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

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

    <xsl:template match="d">

    </xsl:template>

</xsl:stylesheet>

When I apply this sheet, I get output 2 3, which are the remaining textnodes. I've read about the built-in templates which get applied if it can't find a matching template, but in this case, it should find a template?

What is going on?

Edit:

In this case, i would expect to see nothing, because the templates are empty. But i get 2 3 in stead.

Upvotes: 0

Views: 724

Answers (2)

0x6adb015
0x6adb015

Reputation: 7811

When you do <xsl:template match="d">, you tell the processor to ignore all nodes under <d>.

All other nodes are processed with default rules, including the text() one, which is to print the text.

That's why you see 23, and not 1.

Upvotes: 1

Lucero
Lucero

Reputation: 60190

Start from the root:

<xsl:template match="/a">

And specify either a mode (so that the default template does not get called, because it does not find a template for e, f and g) or define your own * template which does nothing at the end of the stylesheet:

<xsl:template match="*"/>

Upvotes: 0

Related Questions