2014-09-29 120 views
0

我的XML代码:显示在XSLT具有特定属性的元素

<?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"但i”中的标题显示详细信息如果没有书名,但有价格的话,会得到不必要的行。这是输出。 感谢您的帮助。

enter image description here

+0

以供将来参考,这是迄今为止在XSLT问题更加有用的,如果你能证明你目前的和/或所需的输出作为实际的HTML/XML源,而不是一个特定浏览器如何呈现它的图片。 – 2014-09-29 17:11:17

回答

1

你需要限制你与for-each那些具有适当的语言标题处理的元素:

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

顺便说一句,你几乎永远不会需要使用text()在XPath表达式中,除非你真的想分别处理单独的文本节点。在像你这样的,你关心的是整个元素的文本内容的情况下,只取value-of元素本身:

<td><xsl:value-of select="price"/></td> 
相关问题