I am sure that my logic is not correct. However I am not sure how to resolve this without multiple for-each tests.
Perhaps a for-each is not the correct way to go? I would appreciate any advice. Additinally I am using XSLT 1.0 in case that is necessary information.
Here is the XML that I am using:
<?xml version="1.0" encoding="utf-8"?>
<FIGURES>
<FIGURE>
<TITLE>Title 1</TITLE>
<DESC>Description 1</DESC>
<CONTENT>Content 1</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 2</TITLE>
<DESC>Description 2</DESC>
<CONTENT>Content 2</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 2</TITLE>
<DESC>Description 2</DESC>
<CONTENT>Content 2</CONTENT>
</FIGURE>
</FIGURES>
Here is the XSLT that I am trying:
<xsl:template match="/">
<div class="container">
<ul class="list">
<xsl:for-each select="//FIGURE">
<li>
<a href="#section-{position()}"><h3><xsl:value-of select="TITLE" /></h3></a>
<p><xsl:value-of select="DESC" /></p>
</li>
<div class="content">
<div id="section-{position()}">
<xsl:value-of select="CONTENT" />
</div>
</div>
</xsl:for-each>
</ul>
</div>
</xsl:template>
Here is the HTML that I want but am not getting:
<div class="container">
<ul class="list">
<li>
<a href="#section-1"><h3>Title 1</h3></a>
<p>Description 1</p>
</li>
<li>
<a href="#section-2"><h3>Title 2</h3></a>
<p>Description 2</p>
</li>
<li>
<a href="#section-3"><h3>Title 3</h3></a>
<p>Description 3</p>
</li>
</ul>
<div class="content">
<div id="section-1">
<p>Content 1</p>
</div>
<div id="section-1">
<p>Content 2</p>
</div>
<div id="section-1">
<p>Content 2</p>
</div>
</div>
</div>
Here is the HTML that I get, but do not want:
<div class="container">
<ul class="list">
<li>
<a href="#section-1"><h3>Title 1</h3></a>
<p>Description 1</p>
</li>
<div class="content">
<div id="section-1">Content 1</div>
</div>
<li>
<a href="#section-2"><h3>Title 2</h3></a>
<p>Description 2</p>
</li>
<div class="content">
<div id="section-2">Content 2</div>
</div>
<li>
<a href="#section-3"><h3>Title 2</h3></a>
<p>Description 2</p>
</li>
<div class="content">
<div id="section-3">Content 2</div>
</div>
</ul>
</div>
EDIT Using Sean’s example I figured it out. I was just missing the select attribute on the tags.
<xsl:template match="/">
<div class="container">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="FIGURES">
<ul class="list">
<xsl:apply-templates select="FIGURE" mode="titles" />
</ul>
<div class="content">
<xsl:apply-templates select="FIGURE" mode="content" />
</div>
</xsl:template>
<xsl:template match="FIGURE" mode="titles">
<li>
<a href="#section-{position()}">
<h3><xsl:value-of select="TITLE" /></h3>
</a>
<p><xsl:value-of select="DESC" /></p>
</li>
</xsl:template>
<xsl:template match="FIGURE" mode="content">
<div id="section-{position()}">
<p><xsl:value-of select="CONTENT" /></p>
</div>
</xsl:template>
This XSLT 1.0 stylesheet…*
…when applied to the referenced input document, will yield…
Update
If you are not using xsl:strip-space (you should, as I gave this to you in the solution), or if for some reason, you have other nodes interspersed between the FIGURE elements, then this style-sheet will give you a more robust solution…