首页 文章

在XSLT中显示具有特定属性的元素

提问于
浏览
0

我的XML代码:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="book.xslt"?>
<bookstore>

<book> 
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>

<book>
<title lang="eng">Learning XML</title>
<price>20.30</price>
</book>

<book>
<title lang="fr">Exploitation Linux</title>
<price>40.00</price>
</book>

</bookstore>

我的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="/">

<html>
<body>
<table border="1">
<tr>
<th>Book Title</th>
<th>Price</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td><xsl:value-of select="title[@lang='eng']/text()"/></td>
<td><xsl:value-of select="price/text()"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我想仅显示具有属性 lang="eng" 的 Headers 的详细信息,但是我没有书名,但输出有's the price. Here' . 谢谢你的帮助 .

enter image description here

1 回答

  • 1

    您需要将使用 for-each 处理的元素限制为具有相应语言 Headers 的元素:

    <xsl:for-each select="bookstore/book[title/@lang = 'eng']">
    

    另外,您几乎不需要在XPath表达式中使用 text() ,除非您确实想要单独处理单个文本节点 . 在像你这样的情况下,你关心的是整个元素的文本内容,只需要 value-of 元素本身:

    <td><xsl:value-of select="price"/></td>
    

相关问题