tags:

views:

33

answers:

3

Suppose I have XML like this:

<xsl:template match="assessment | section ">
             .
             .
             .
</xsl:template>

I do this because I mostly want to treat assessment and section nodes the same. However, I do want to treat them a little differently. How can I tell whether a match was for assessment or for section?

A: 

Do something like this:

<xsl:if test="name()='section'">
</xsl:if>
Paul Reiners
A: 

You may test for:

self::assessment

which is slightly more efficient than using the name() function.

However in cases like this I will put the common code in another template (either named or in a named mode) and will instantiate the common processing like this:

<xsl:apply-templates select="." mode="common"/>
Dimitre Novatchev
A: 

Well, depending on precisely what you're doing, you may be able to factor out the common stuff into a separate named template:

<xsl:template match="assessment">
  <xsl:call-template name="commonBit" />
  <!-- stuff specific to assessments -->
</xsl:template>

<xsl:template match="section">
  <xsl:call-template name="commonBit" />
  <!-- stuff specific to sections -->
</xsl:template>

<xsl:template name="commonBit">
  <!-- stuff common to both assessments and sections -->
</xsl:template>

Obviously, the call-template element can go anywhere in the assessment/section templates; the context node is the same while processing the 'commonBit' template as it was when you process the assessment/section templates.

Flynn1179