Question

I am trying to do an XSL choose to populate an select box. There will be more options in the box once I have this one working I just need to get this working first. It is loading properly it just isn't pulling the e:name so the option box is rendering but is blank with no options.

Here is my XSL

<xsl:template match="/">

<html>
<body>


<select id="epselect">
<xsl:element name="option">
<xsl:choose>
<xsl:when test="dalehoward/e:ep/@id = 1">
<xsl:value-of select="e:name"/>
<xsl:attribute name="value">1</xsl:attribute>
</xsl:when>
<xsl:otherwise>
EP not found
</xsl:otherwise>
</xsl:choose>
</xsl:element>

</select>

</body>
</html>
</xsl:template>

Here is my XML

<dalehoward         xmlns:artist="http://www.beatport.com/artist/dale-howard/78784"
                xmlns:e="http://www.beatport.com/artist/dale-howard/78784/releases"
                xmlns:t="http://www.beatport.com/artist/dale-howard/78784/tracks">



<artist:bio>
<artist:profilepic>profilepic.jpg</artist:profilepic>
<artist:dob>16/10/1987</artist:dob>
<artist:pob>Liverpool</artist:pob>
<artist:about>
English Deep House producer, Dale Howard, first burst onto the scene in 2009 with his debut EP on Neurotraxx Deluxe 'Gotta Be Deep', which topped the Beatport Deep House chart reaching the Number 1 spot. Since then he has been making waves with an array of releases on world renowned labels like Fear Of Flying, Loco Records, Off Recordings and many others. Aswell as having countless top 20's and 50's Dale has also reached Number 2 and Number 3 on the Beatport Deep House Chart with his tracks 'Dropout' and '4 Hour Bang', which also stayed in the Top 10 for 9 weeks. 2012 brought more success and high acclaim for Dale, with his productions gaining huge support from artists like DJ Sneak, Luciano, Reboot, Dyed Soundorom, Steve Lawler, Dubfire, Magda, M.A.N.D.Y, Homework, Huxley, Jordan Peak, Dusky, Robert Owens, Michelle Owen and loads more, as well as even more releases scheduled with world renowned record labels.
</artist:about>
</artist:bio>

<e:ep id="1">
    <e:name>Letters EP</e:name>
    <e:year>2012</e:year>
    <e:label>Static Audio</e:label>
    <e:image>letters.jpg</e:image>

        <t:track number="1" beatportrank="0">
            <t:name>Letters</t:name>
            <t:length>6.35</t:length>
            <t:ytubelink>2H2XDQqvbpc</t:ytubelink>
        </t:track>
        <t:track number="2" beatportrank="0">
            <t:name>Later</t:name>
            <t:length>7.56</t:length>
            <t:ytubelink>w61RrgBPahk</t:ytubelink>
        </t:track>
            <t:track number="3" beatportrank="0">
            <t:name>'89 Flava</t:name>
            <t:length>7:38</t:length>
            <t:ytubelink>Mgarl-FlVhQ</t:ytubelink>
        </t:track>
        <t:track number="4" beatportrank="0">
            <t:name>Safe Presentation</t:name>
            <t:length>7.55</t:length>
            <t:ytubelink>d_U38G9AwHk</t:ytubelink>
        </t:track>
</e:ep>
</dalehoward>
Was it helpful?

Solution

Inside of your xsl:when the context node is still /, so the expression <xsl:value-of select="e:name"/> is the equivelent of /e:name, which will never match anything.

You need to adjust the XPath relative from the context node <xsl:value-of select="dalehoward/e:ep[@id=1]/e:name" />.

Also, you need to move the xsl:value-of below the generation of the attribute. Otherwise, you will get an error that you cannot create attributes after a child of the containing element.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:artist="http://www.beatport.com/artist/dale-howard/78784"
    xmlns:e="http://www.beatport.com/artist/dale-howard/78784/releases"
    xmlns:t="http://www.beatport.com/artist/dale-howard/78784/tracks">
    <xsl:template match="/">      
        <html>
            <body>
                <select id="epselect">
                    <xsl:element name="option">
                        <xsl:choose>
                            <xsl:when test="dalehoward/e:ep/@id = 1">
                                <xsl:attribute name="value">1</xsl:attribute>
                                <xsl:value-of select="dalehoward/e:ep[@id=1]/e:name"/>
                            </xsl:when>
                            <xsl:otherwise>
                                EP not found
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:element> 
                </select> 
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top