Appendix answer key template Goal:


Extract all Appendix Titles
If Appendix Title is Answer Key call custom solutions template
If Not Answer Key call default appendix template


XML:

&lt;appendix&gt;
  &lt;title&gt;Appendix A&lt;/title&gt;
  &lt;section&gt;data&lt;/section&gt;
&lt;/appendix&gt;

&lt;appendix&gt;
  &lt;title&gt;Answer Key&lt;/title&gt;
  &lt;section&gt;data&lt;/section&gt;
&lt;/appendix&gt;


XSLT:

&lt;xsl:template match="appendix/title"&gt;
  &lt;xsl:choose&gt;
    &lt;xsl:when test="self::node()[text()='Answer Key']"&gt;
       &lt;div class="answer-key"&gt;
       &lt;xsl:call-template name="solutions"/&gt;
       &lt;/div&gt;
    &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;
      &lt;xsl:apply-templates select="appendix"/&gt;
    &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;  
&lt;/xsl:template&gt;


This template works correctly; however I am interested in gaining some ground when it comes to writing solid XSL templates. Particularly the XPATH expressions. There seem to be various ways to achieve the same results when using XPath. Is the following XPath expression best?

test="self::node()[text()='Answer Key']"
I don't see how that &lt;xsl:otherwise&gt; would ever do anything based on your sample xml. The template matches on appendix/title so your context would be the title element. Applying templates to appendix children of title would be a no-op. I think something like this would be closer to what you want

&lt;xsl:template match="appendix[title = 'Answer Key']"&gt;
  &lt;div class="answer-key"&gt;
    &lt;xsl:call-template name="solutions"/&gt;
  &lt;/div&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="appendix"&gt;
  &lt;!-- Do something standard --&gt;
&lt;/xsl:template&gt;


Here, the first template has a higher implicit priority than the second template so it will match on the "Answer Keys". If the predicate evaluates to false, the second template will then match. Note though the code above will behave differently if the called template "solutions" depends on having a title element context node as it would in your code sample. In this sample the context would be the appendix element, which seems like possibly a more appropriate point in the document hierarchy to be performing whatever operations you intend to do.