2014-09-29 116 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> 
相關問題