0

下面是我的代码示例。项目标签多次循环我的代码。

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

1 回答 1

0

<rss>对于输入 xml(我更正了一些明显的错误,例如“htt”->“http”或开始标签中的分号)

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

于 2013-08-30T11:10:15.067 回答