Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Level: Beginner

Goal: 1- Extract all Appendix Titles 2- If Appendix Title is Answer Key call custom solutions template 3. If Not Answer Key call default appendix template

XML:

<appendix>
  <title>Appendix A</title>
  <section>data</section>
</appendix>

<appendix>
  <title>Answer Key</title>
  <section>data</section>
</appendix>

XSLT:

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

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']"

Thanks ahead of time.

share|improve this question

2 Answers

up vote 3 down vote accepted

I don't see how that <xsl:otherwise> 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

<xsl:template match="appendix[title = 'Answer Key']">
  <div class="answer-key">
    <xsl:call-template name="solutions"/>
  </div>
</xsl:template>

<xsl:template match="appendix">
  <!-- Do something standard -->
</xsl:template>

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.

share|improve this answer
Thanks. Your code example was nice and clean and your explanation made me revisit this code and see that logically it was not sound, as you had mentioned. – Jessica Burnett Nov 7 '12 at 11:34

Is the following XPath expression best? test="self::node()[text()='Answer Key']"

You can simplify the XPath expression to .='Answer Key'

share|improve this answer
Thanks for the XPath pointers and explanations your expression is much more clean and neat. – Jessica Burnett Nov 7 '12 at 11:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.