1

Xslt apply-templates selects all other text files

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 am trying 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 text boxes. I've read about built-in patterns that apply if no matching pattern can be found, but in which case it should find a pattern?

What's happening?

Edit:

In this case, I didn't expect to see anything, because the templates are empty. But I am getting 2 3 instead.

0


a source to share


2 answers


When you execute <xsl:template match="d">

, you tell the processor to ignore all nodes in the <d>

.

All other nodes are processed using the default rules, including text()

which is for printing text.



This is why you see 23 and not 1.

+1


a source


Start at the root:

<xsl:template match="/a">

      



And specify either mode (so the default template won't be called because it won't 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="*"/>

      

0


a source







All Articles