Friday, 30 January 2015

XSLT template does not apply to all elements



I am confused about the use of XSLT templates and when/how they are applied. Suppose I have the following XML file:



<book>
<chapter> 1 </chapter>
<chapter> 2 </chapter>
</book>


and I'd like to match all chapters in order. This is a XSLT stylesheet:



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR" >
<xsl:template match="book">
<h1>book</h1>
</xsl:template>
<xsl:template match="chapter">
<h2>chapter <xsl:value-of select="."/></h2>
</xsl:template>
</xsl:stylesheet>


The result of the stylesheet is



<h1>book</h1>


without the expected numeration of chapters. Adding an <xsl:apply-templates /> at the end of the book matching template didn't help. I'd like to do without an xls:for-each though.


No comments:

Post a Comment