I am creating a html page with xslt to format my xml into a html page, however I am getting a value repeated and I can’t find why, below are my xsl, xml, and html. I have indicated where my repeat value occurs directly below, Thanks for everyone’s help!
<fieldset>
<legend>Joys of a MAD man</legend><ol>
Joys of a MAD man *********** Why is the title repeated? ************
<li>Slow Moving<a href="./Music/Splintz - Joys of a MAD Man/Slow Moving.mp3">
[Download]
</a></li>
</ol></fieldset>
My XML
<albums>
<album>
<title>Joys of a MAD man</title>
<track>Slow Moving</track>
</album>
<album>
<title>Single</title>
<track>None</track>
</album>
</albums>
and finally my xsl
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="album">
<fieldset>
<legend><xsl:value-of select="title/text()" /></legend>
<ol>
<xsl:apply-templates />
</ol>
</fieldset>
</xsl:template>
<xsl:template match="track">
<li>
<xsl:value-of select="text()" />
<a>
<xsl:attribute name="href">
<xsl:text>./Music/Splintz - Joys of a MAD Man/</xsl:text>
<xsl:value-of select="text()"/>
<xsl:text>.mp3</xsl:text>
</xsl:attribute>
[Download]
</a>
</li>
</xsl:template>
</xsl:stylesheet>
There are built-in default template rules that copy text to the result document.
<xsl:apply-templates/>is short for<xsl:apply-templates select="child::node()"/>.You used
xsl:apply-templatesinside the template match foralbum. When you are “standing” on thealbumelement,titleis one of the child nodes that get processed.The built-in template matched
titleoutput it’stext()“Joys of a MAD man” the second time.There are a number of ways to prevent the
titletext from being output the second time. You could:titleto your stylesheet to prevent the built-in template from matching:<xsl:template match="title"/>titlefrom yourapply-templates:<xsl:apply-templates select="node()[not(self::title)]"/>trackchild elements:<xsl:apply-templates select="track"/>