Federico
Federico

Reputation: 3920

How are XSL templates applied?

I have this xml:

<temas>
  <tema>
    <titulo>Odio como a un burgués la fuga de las sábanas.</titulo>
    <nav>
    <menu>
      <menu-item>One</menu-item>
      <menu-item>Two </menu-item>
      <menu-item>Three</menu-item>
      </menu>
    </nav>
    <content>
      <span>hi</span>
      <span>hi</span>
  </content>
  <pie>
    <prev>something</prev>
    </pie>
  </tema>
</temas>

To which I have applied this XSL transformation:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
</xsl:template>

<xsl:template match="content">
  <xsl:copy-of select="./*" />
</xsl:template>

<xsl:template match="span" priority="10000">
    Why I don't show up?!
</xsl:template>

</xsl:stylesheet>

In my limited knowledge of XSLT, I would have expected that the last template would replace the span nodes, but that doesn't happen. I hoped that the identity transform would copy everything and apply all the templates that matched, but it fails to apply that last template. I tried to change the priority to no avail.

Can you explain how XSL works in this cases when applying multiple templates?

Upvotes: 1

Views: 96

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

I would have expected that the last template would replace the span nodes, but that doesn't happen.

It doesn't happen because the last template is never applied. It isn't applied because the template matching the parent content node does not apply templates. You would see a different result if instead of:

<xsl:template match="content">
  <xsl:copy-of select="./*" />
</xsl:template>

you would do:

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

See a more detailed explanation here:
Why does processing a parent element's template prevent the processing of child element templates?

Upvotes: 3

Related Questions