XSL选择不正确提取数据的情况

时间:2014-05-14 23:11:07

标签: html xml xslt xsl-choose

我正在尝试做一个XSL选择填充一个选择框。一旦我开始工作,盒子里会有更多的选项我只需要先让它工作。它正在正确加载它只是没有拉e:name所以选项框正在渲染但是没有选项的空白。

这是我的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>

这是我的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>

1 个答案:

答案 0 :(得分:0)

在您的xsl:when内部节点仍然是/,因此表达式<xsl:value-of select="e:name"/>/e:name的等价物,它永远不会匹配任何内容。

您需要从上下文节点<xsl:value-of select="dalehoward/e:ep[@id=1]/e:name" />调整XPath相对值。

此外,您需要将xsl:value-of移动到属性生成的下方。否则,您将收到一个错误,您无法在包含元素的子元素之后创建属性。

<?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>