tags:

views:

197

answers:

2

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"&gt;
 <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.

A: 

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="*"/>
Lucero
No difference..
Ikke
+1  A: 

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.

0x6adb015
The weird thing is that if i only have one apply-templates, it just works as it's supposed to work. Just selects b node in this example. But as soon as a put another apply-templates, it shows al the textnodes.
Ikke
Ah ok, so if i just went on, and specified templates for the other nodes as well, it would just be fixxed?
Ikke
If do not want to see any text, do <xsl:apply-template select="b"/> or replace the default rules with <xsl:template match="text()" />.
0x6adb015