2013-12-24 44 views
0

我在使用XPath和包含函数时遇到问题。试想一下,下面的XML例子:xpath包含()函数的工作方式

<movie-selection> 
<film> 
    <name>Hunger Games - Catching Fire</name> 
    <release>2013</release> 
    <description>Katniss Everdeen and Peeta Mellark are once again forced to fight for there lifes in the Hunger Games. There Victory the previous year kick starts a rebellion againist the Capitol and President Snow. 
    </description> 
    <runtime>146 minutes</runtime> 
    <stars>Jennifer Lawrence, Josh Hutcherson, Liam Hemsworth</stars> 
    <genre>Adventure, Sci-Fi</genre>   
<rating>4.8</rating> </film> 

<film> 
    <name>Avatar</name> 
    <release>2009</release> 
    <description>A paraplegic Marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home. 
    </description> 
    <runtime>162 minutes</runtime> 
    <stars> Sam Worthington, Zoe Saldana, Sigourney Weaver</stars> 
    <genre>Adventure, Fantasy, Action</genre> 
    <rating>4.5</rating> 

我想回到那个有“行动”在类型属性这所有的电影是什么,我现在有,但它是不是加工。任何人都能看到我的错误?

movie-selection/film[contains(genre, 'Action')] 

任何帮助表示赞赏。

<xsl:for-each select="movie-selection/film"> 
    <xsl:if test="/movie-selection/film[contains(genre, &apos;Drama&apos;)]"> 
     <p><xsl:value-of select="name"/></p> 
     <p><xsl:value-of select="release"/></p> 
     <p><xsl:value-of select="runtime"/></p> 
     <p><xsl:value-of select="description"/></p> 
     <p><xsl:value-of select="stars"/></p> 
     <p><xsl:value-of select="genre"/></p> 
     <p><xsl:value-of select="rating"/></p> 
    </xsl:if> 
</xsl:for-each> 

它返回一切。

+1

在你的榜样,1)'genre'不是一个属性,但是一个元素2)没有'genre'包含的行动值”。 – BoltClock

+0

我只看到缺少的是一个主要的斜线,例如'/ movie-selection/film [contains(genre,'Action')]''。将其更改为* Adventure *并获得一个匹配,请参阅http://www.xpathtester.com/obj/65cee161-dbc1-46a3-a29a-ab1b4360fee9 – Phil

+0

它似乎在路径测试程序中正常工作而不是xsl文件 – Nic

回答

1

现在我们知道上下文,这很容易。只需将条件放入for-each选择器即可。

<xsl:for-each select="movie-selection/film[contains(genre, 'Drama')]"> 
    <p><xsl:value-of select="name" /></p> 
    <!-- and so on --> 
</xsl:for-each> 

为什么打扰你永远不需要的模板节点?如果你真的想用一个测试循环内,使用该

<xsl:for-each select="movie-selection/film"> 
    <xsl:if test="contains(genre, 'Drama')"> 
+0

谢谢。我对XML的工作非常陌生,尽管Dreamweaver仍然在学习很多东西。 – Nic

+0

您的代码不起作用的原因是,以“/”开头的路径搜索整个文档,而您想要在当前正在处理的元素(称为“上下文节点”)内搜索。 –

+0

@MichaelKay我认为你评论了错误的帖子。我的代码工作正常 – Phil