我在使用XPath和包含函数时遇到问题。想象一下下面的XML示例:
<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, 'Drama')]">
<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>
它会返回所有内容。
答案 0 :(得分: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')">