2

What concept am I missing that I'm not getting what I'm expecting? Also, why would @field be blank (not showing 'location') when matching the 2nd time around?

xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <!-- sample XSLT snippet -->
  <xsl:template match="xml">
    <xsl:apply-templates select="*" />
    <!-- three nodes selected here -->
    <xsl:call-template name="rshandle" />

  </xsl:template>

  <xsl:template match="foo">
    <!-- will be called once -->
    <xsl:text>
      foo element encountered
    </xsl:text>
  </xsl:template>

  <xsl:template match="*">
    <!-- will be called twice -->
    <xsl:text>
      other element ecountered
    </xsl:text>
  </xsl:template>


  <xsl:template name="rshandle" match="foo">
    <!-- will be called once -->
    <xsl:value-of select="@field" />
    <xsl:text>
      oops i did it again!
    </xsl:text>
  </xsl:template>

</xsl:stylesheet>  

xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="calltemplatematch.xslt"?>
<!-- sample XML snippet -->
<xml>
  <foo field="location"/>
  <bar />
  <baz />
</xml>

expecting

      other element ecountered

      other element ecountered

location
      oops i did it again!

actual

location
      oops i did it again!

      other element ecountered

      other element ecountered

      oops i did it again!
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Which part do you need to be explained? – michael.hor257k Jun 01 '17 at 15:10
  • I updated OP, to be a little more specific. I guess for starters, why is @field blank (instead of showing location string) on the second instance that appears at the bottom? – Rod Jun 01 '17 at 15:14
  • I am afraid you'll need to pose a specific question for each item you don't understand. – michael.hor257k Jun 01 '17 at 15:19
  • No worries, will do – Rod Jun 01 '17 at 15:20
  • 2
    Note that you have two templates matching "foo" which is considered an error (See https://www.w3.org/TR/xslt#conflict). If the XSLT processor is not signalling the error it will pick the last matching template in the stylesheet. So, the template that outputs "foo element encountered" will not get used. – Tim C Jun 01 '17 at 15:23

2 Answers2

2

To meet your expectations, the XSL should look like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="text"/>

    <!-- sample XSLT snippet -->
    <xsl:template match="xml">
        <xsl:apply-templates select="*"/>
        <!-- three nodes selected here -->
        <xsl:apply-templates select="foo" mode="rshandle"/>
    </xsl:template>

    <xsl:template match="foo"/>

    <xsl:template match="*">
        <!-- will be called twice -->
        <xsl:text>other element ecountered</xsl:text>
    </xsl:template>

    <xsl:template match="foo" mode="rshandle">
        <!-- will be called once -->
        <xsl:value-of select="@field"/>
        <xsl:text>oops i did it again!</xsl:text>
    </xsl:template>

</xsl:stylesheet>

Also, why would @field be blank (not showing 'location') when matching the 2nd time around?

Because you used <xsl:call-template>. The good explanation from another answer:

A concept to understand with XSLT is that of the "current node". With <xsl:apply-templates> the current node moves on with every iteration, whereas <xsl:call-template> does not change the current node. I.e. the . within a called template refers to the same node as the . in the calling template. This is not the case with apply-templates.

sergdenisov
  • 8,327
  • 9
  • 48
  • 63
1

why is @field blank on the second instance that appears at the bottom?

Because when the "rshandle" template is called, it is called from the context of the xml root element - which does not have a field attribute. Calling a template does not change the current context - unlike applying templates.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51