如何选择XSL中的每个第二个节点

时间:2013-08-30 10:58:51

标签: xslt

以下是我的代码示例。 item标签循环遍历我的代码。

<item>
    <title>Secret Court Palace Gardens</title>`enter code here`
    <link>htt://www.flickr.com/photos/aataka/954934842/in/pool-1540822@N20</link>
    <description>
        <p>
            <a href="htt://www.flickr.com/people/aataka/">aataka</a> has added a photo to the pool:</p>
        <p>
            <a href="htt://www.flickr.com/photos/aataka/954934842/" title="Hampton Court Palace Gardens">
                <img src="htt://farm4.staticflickr.com/3802/954934842_a38fca47c7_m.jpg" width="240" height="107" alt="Secret Court Palace Gardens" />
            </a>
        </p>
    </description>
</item>

我需要在第二个'a'标记内的'img src'标记中选择url值。我尝试了类似下面的东西,但没有奏效。有人可以帮忙吗?非常感谢。

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="htt://www.w3.org/1999/XSL/Transform" xmlns:media="htt://search.yahoo.com/mrss/">
<xsl:output method="html" />
<xsl:template match="/">

    <xsl:for-each select="rss/channel/item/description/p[2]/*">
        <img src="{a/@href}" style="margin:5px 5px" />
    </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

输入xml(我更正了一些明显的错误,例如“htt” - &gt;“http”或<rss>开头标记中的分号)

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="search.yahoo.com/mrss/" xmlns:dc="purl.org/dc/elements/1.1/" xmlns:creativeCommons="cyber.law.harvard.edu/rss/creativeCommonsRssModule.html" xmlns:flickr="urn:flickr:user">
    <channel>
        <item>
            <title>Secret Court Palace Gardens</title>
            <link>htt://www.flickr.com/photos/aataka/954934842/in/pool-1540822@N20</link>
            <description>
                <p>
                    <a href="http://www.flickr.com/people/aataka/">aataka</a> has added a photo to the pool:</p>
                <p>
                    <a href="http://www.flickr.com/photos/aataka/954934842/" title="Hampton Court Palace Gardens">
                        <img src="http://farm4.staticflickr.com/3802/954934842_a38fca47c7_m.jpg" width="240" height="107" alt="Secret Court Palace Gardens"/>
                    </a>
                </p>
            </description>
        </item>
    </channel>
</rss>

以下xslt应该可以正常工作

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:media="htt://search.yahoo.com/mrss/">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title/>
            </head>
            <body>
                <xsl:for-each select="rss/channel/item/description/p[position() mod 2 = 0]//img">
                    <img src="{@src}" style="margin:5px 5px"/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

它产生以下输出

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <img src="http://farm4.staticflickr.com/3802/954934842_a38fca47c7_m.jpg" style="margin:5px 5px">
    </body>
</html>

我认为p[position() mod 2 = 0]应该做一份工作,因为我想你想要选择每一秒<p>元素并迭代所有包含的<img>