XSLT中相同标记的多链接遍历

时间:2012-09-17 06:29:06

标签: xslt-1.0 xslt-2.0

您好

我有xml以下,我正在尝试编写xslt。在这个XML中,有些书可能有Movie标签。所以每当标签是内联数据和内容类型是电影时我必须遍历该标签,它应该显示为

第一个链接

(本书的Movie可在此处获得。)

第二个链接

(图片图标)Movie

因此每本图书将有两个链接,将显示在网站的不同位置。我的问题是如何在遍历完成后保持电影名称的跟踪。我无法第二次浏览同一本书。如何使用变量或标志。请任何人都可以提供相同的XSLT。

输入XML

<Book><body>
<movie  xmlns:xlink="http://www.w3.org/1999/xlink">
    <caption>
        <p>Testing data</p>
        <p>
            (A
            <inline-data
                content-type="Movie"  xlink:href="video1.mpg"
                xlink:title="Movie" xlink:type="simple">Movie
            </inline-data>
            of this Part is available here.)
        </p>
    </caption>
</movie>
<movie  xmlns:xlink="http://www.w3.org/1999/xlink">
    <caption>
        <p>Testing data</p>
        <p>
            (This movie
            <inline-data
                content-type="Movie"  xlink:href="video2.mpg"
                xlink:title="Movie" xlink:type="simple">Movie
            </inline-data>
             is available here.)
        </p>
    </caption>
</movie>
</body></Book>

1 个答案:

答案 0 :(得分:0)

可能你看起来像这样:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <xsl:output indent="yes"/>
  <xsl:template match="Book">
    <xsl:choose>
      <xsl:when test="body/movie">
        <xsl:for-each select="//movie">
          <p><xsl:apply-templates select="caption/p[2]"/></p>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <html>
          <head><title>Movie</title></head>
          <body>
            <xsl:text>No Movie present</xsl:text>
          </body>
        </html>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="inline-data">
    <a href="{@xlink:href}"><xsl:apply-templates/></a>
  </xsl:template>
</xsl:stylesheet>