<library>
<thriller>
<book>
<ISBN>1000</ISBN>
<title>Sherlock Holmes</title>
<author>Bob Dylan</author>
<publisher>BBC</publisher>
<country>England</country>
<price>10.90</price>
<edition>5</edition>
<year>2005</year>
</book>
<book>
<ISBN>1001</ISBN>
<title>The Indian Girl</title>
<author>Chetan Bhagat</author>
<publisher>Anusha Publishers</publisher>
<country>India</country>
<price>1270</price>
<edition>3</edition>
<year>2005</year>
</book>
</thriller>
</library>
<xsl:for-each select="library/thriller/book"> then
<tr bgcolor="#9acd32">
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:for-each>
<xsl:for-each select="library/thriller>
<xsl:for-each select="book">
</xsl:for-each>
</xsl:for-each>
在上面的例子中我想打印惊悚片库中的所有书籍内容。我试过了 但它不会作为一张桌子显示出来。我之前已经给出了所有必要的表格标签。所以我试过了\
but whether there is away to do that in single loop. thanks in advance.
答案 0 :(得分:0)
这里有一个工作示例,只有一个循环。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="library">
<table>
<xsl:for-each select="thriller/book">
<tr bgcolor="#9acd32">
<td><xsl:value-of select="ISBN"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="/">
<hmtl>
<head>
<title>Books</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</hmtl>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>
请注意:
library
匹配。for-each
中的(相对)XPath是内部元素,即
thriller/book
。