1

我正在尝试做一个 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>
4

1 回答 1

0

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

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

此外,您需要将属性的生成移到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>
于 2014-05-14T23:25:02.333 回答