xml - 如何使用 XSLT 遍历 xml 文档中的元素

标签 xml xslt altova

我正在使用 XSLT 遍历 XML 文档。
文档的根节点称为movies,它可以包含一个或多个movie元素。但是,这些 movie 元素包含除 actor 元素之外的单个元素。您如何循环显示多个 Actor ?我已经在使用 <xsl:for-each>循环播放每部电影
这是 xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="/movies">

<html>
    <head>
    <link rel="stylesheet" href="stylecss.css" />
        <title>My Movies Collection</title>
    </head>
    <body>
        <!-- Use for each loop to extract all child nodes and their information-->

    <xsl:for-each select="/movies/movie">
    <!--Add title as well as year-->
        <h1><xsl:value-of select="title" />
        <xsl:text> ( </xsl:text>
        <xsl:value-of select="@year" />
        <xsl:text> ) </xsl:text>

        </h1>
        <p>
        <img>
            <xsl:attribute name="src">
            <xsl:value-of select="poster" />
            </xsl:attribute>
        </img>
        <span id="details">

        <xsl:text>Review: </xsl:text>
        <xsl:value-of select="@review" />
        <xsl:text>, Type: </xsl:text>
        <xsl:value-of select="@type" />
        <br></br>

        <xsl:text>Producer: </xsl:text>
        <xsl:value-of select="producer" /> 
        <br/>

        <xsl:text>Actor: </xsl:text>
        <xsl:value-of select="actor"/> <br/>

        <xsl:text>Director: </xsl:text>
        <xsl:value-of select="director" /><br/>
        <br/>


<i>         <xsl:text>Comments: </xsl:text>
        <xsl:value-of select="comments" /> </i>



        </span>
        </p>

    </xsl:for-each>

    </body>

</html>



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



xml 文档本身:

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="stylesheet.xslt" ?>
<movies>
  <movie type="comedy" rating="PG" review="4" year="1997">
  <title>How To Be A Player</title>
  <writer>Mark Brown</writer>
  <producer>Todd R. Baker</producer>
  <director>Russell Simmons</director>
  <actor>Bill Bellamy</actor>
  <actor>Mari Morrow</actor>
  <poster>img/htbap.jpeg</poster>
  <comments>Promotes polygamy</comments>  
  </movie>


  <movie type="comedy" rating="PG" review="5" year="2010">
  <title>Grown Ups</title>
  <writer>Adam Sandler</writer>
  <producer>Jack Giarraputo</producer>
  <director>Fred Wolf</director>
  <actor>Chris Rock</actor>
  <actor>Rob Schneider</actor>
  <poster>img/gu.jpeg</poster>
  <comments>Funny stuff</comments>  
  </movie>

 <movie type="drama" rating="PG" review="5" year="2010">
  <title>The Social Network</title>
  <writer>Dana Brunetti</writer>
  <producer>Scott Rudin</producer>
  <director>David Fincher</director>
  <actor>Jesse Eisenberg</actor>
  <actor>Justin Timberlake</actor>
  <poster>img/sn.jpeg</poster>
  <comments>So innovative</comments>  
  </movie>

  <movie type="comedy" rating="PG" review="5" year="2012">
        <title>Project X</title>
        <writer>Nima Nourizadeh</writer>
        <producer>Todd Phillips</producer>
        <director>Nima Nourizadeh</director>
        <actor>Thomas Mann</actor>
        <actor>Oliver Cooper</actor>
        <poster>img/px.jpeg</poster>
        <comments>Best party to ever</comments>
    </movie> 

</movies>

最佳答案

没有理由可以嵌套 xsl:for-each在另一个内。所以你可以替换 <xsl:value-of select="actor"/> ,它只输出第一个 Actor 的文本,用这个

 <xsl:for-each select="actor">
     <xsl:value-of select="." /><br />
 </xsl:for-each>

请注意 select 中的 xpath 表达式是一个相对表达式;它是相对于 movie您当前所在的节点,因此将选择所有 actor作为它的子元素的元素。 (这也意味着您当前的 <xsl:for-each select="/movies/movie"> 实际上可能只是 <xsl:for-each select="movie"> )

话虽如此,在 XSLT 中,使用基于模板的解决方案通常比使用 xsl:for-each 更好。因为那时您可能会重复使用模板,例如,如果您有多个作家或导演。

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/movies">
<html>
    <head>
    <link rel="stylesheet" href="stylecss.css" />
        <title>My Movies Collection</title>
    </head>
    <body>
        <xsl:apply-templates select="movie" />
    </body>
</html>
</xsl:template>

<xsl:template match="movie">
    <h1>
        <xsl:value-of select="title" />
        <xsl:text> ( </xsl:text><xsl:value-of select="@year" /><xsl:text> ) </xsl:text>
    </h1>
    <p>
        <img src="{poster}"/>
        <span id="details">
            <xsl:text>Review: </xsl:text>
            <xsl:value-of select="@review" />
            <xsl:text>, Type: </xsl:text>
            <xsl:value-of select="@type" />
            <br></br>

            <xsl:text>Producer: </xsl:text>
            <xsl:apply-templates select="producer" />
            <br/>

            <xsl:text>Actor: </xsl:text>
            <xsl:apply-templates select="actor" />
            <br/>

            <xsl:text>Director: </xsl:text>
            <xsl:apply-templates select="director" />
            <br/>

            <i>         
                <xsl:text>Comments: </xsl:text>
                <xsl:value-of select="comments" />
            </i>
        </span>
    </p>
</xsl:template>

<xsl:template match="movie/*">
    <xsl:value-of select="."/> <br/>
</xsl:template>
</xsl:stylesheet>

另请注意在创建 img 的源代码时使用属性值模板元素

<img src="{poster}"/>

花括号表示要计算的表达式,而不是字面输出。

关于xml - 如何使用 XSLT 遍历 xml 文档中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422597/

相关文章:

c# - 是否有使用 .NET 的转换引擎或库?

xml - Notepad++ XML Display on separate lines like Internet Explorer Display Of XML

sql-server - 根据条件消除 XML 中的行

java - 转换 XML 以声明根元素上的所有命名空间

xslt - XPath轴之前的异常行为

xml - 在 XSLT 转换中保留属性之间的空格

xml - 从不同的 XML 模式映射 XML 数据

jquery - 有没有办法在 jQuery 中获取类似 JSONP 的 XML 提取?

xml - xsd-schema 中的自定义属性

java - 如何强制 xmlspy 代码在元素上写出合格的 namespace ?