我有XML,所以:
<Root>
<ID>NSA</ID>
<Groups>
<Group>
<ID>Europe</ID>
<Levels>
<Level>
<RootLevelID>Cases B</RootLevelID>
<Faults>
<Fault>
<FaultID>case 1</FaultID>
</Fault>
<Fault>
<FaultID>case 2</FaultID>
</Fault>
</Faults>
</Level>
</Levels>
</Group>
</Groups>
</Root>
出于可读性的考虑,我使用以下XSL将其设置为html:
<xsl:stylesheet version="1.0">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Output</title>
</head>
<body>
<xsl:for-each select="//Root">
<Table border="1">
<Th>
<xsl:value-of select="ID"/>
</Th>
<Tr>
<td>
<xsl:for-each select="current()//Group">
<xsl:for-each select="current()//Level">
<tr>
<td>
<xsl:value-of select="current()//RootLevelID"/> Level name <xsl:for-each
select="current()//Fault"> <td>
<xsl:value-of select="FaultID"/> Fault name </td> </xsl:for-each>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</td>
</Tr>
</Table>
<br/>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
但我只会得到第一个错误成员,而不是所有成员,即使它在for-each循环中。它只输出"case 1“。
但是,由于这是更大上下文的一部分,前两个for-each循环(Root和Group )正确地迭代了xml中的所有组成员。
也许嵌套的for-each循环在XPATH中不能很好地工作?
发布于 2014-01-01 02:25:39
正如@Tim C所指出的,您的xslt不是很优雅,但确实可以工作。需要注意的是,我不确定为什么要使用current(),因为您可以轻松地按文档顺序处理xml:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Output</title>
</head>
<body>
<xsl:for-each select="/Root">
<table border="1">
<th>
<xsl:value-of select="ID"/>
</th>
<tr>
<td>
<xsl:for-each select="Groups/Group">
<xsl:for-each select="Levels/Level">
<tr>
<td>
<xsl:value-of select="RootLevelID"/>
<xsl:text> Level name</xsl:text>
<xsl:for-each select="Faults/Fault">
<td>
<xsl:value-of select="FaultID"/>
<xsl:text>Fault name </xsl:text>
</td>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</td>
</tr>
</table>
<br/>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
https://stackoverflow.com/questions/20857264
复制相似问题