[英]XML to Table <for-each>
I am trying to turn the following into a table, but I am having a hard time understanding how it parses through all of the child nodes.我正在尝试将以下内容转换为表格,但我很难理解它是如何解析所有子节点的。 Will it go through each element until the last one or do I need to have a for-each for each parent node?
它会通过每个元素 go 直到最后一个元素,还是我需要为每个父节点设置一个 for-each ? Also, if there are multiple occurrences of "capris" allowed, do I need to have cells for each possible occurrence (say if there are 3 max but I only have 2, then I would still need 3 cells displayed).
此外,如果允许多次出现“capris”,我是否需要为每个可能出现的单元格设置单元格(例如,如果最多有 3 个但我只有 2 个,那么我仍然需要显示 3 个单元格)。
XML: XML:
<Persons xmlns = "">
<Person>
<Shirts>One</Shirts>
<Pants>
<Jeans>
<Shorts>One</Shorts>
<Capris>One</Capris>
<Capris>Two</Capris>
</Jeans>
<Dress>One</Dress>
</Pants>
</Person>
<Person>
<Shirts>One</Shirts>
<Pants>
<Jeans>
<Shorts>One</Shorts>
<Capris>One</Capris>
<Capris>Two</Capris>
<Capris>Three</Capris>
</Jeans>
<Dress>One</Dress>
</Pants>
</Person>
</Persons>
XSL: XSL:
<table border="1">
<tr bgcolor="yellow">
<td><b>Shirts</b></td>
<td><b>Shorts</b></td>
<td><b>Capris</b></td>
<td><b>Capris</b></td>
<td><b>Capris</b></td>
</tr>
<xsl:for-each select="Persons">
<xsl:sort select="Persons/Persib" />
<tr style="font-size: 10pt; font-family: verdana">
<td><xsl:value-of select="Shirts"/></td>
<td><xsl:value-of select="Shorts"/></td>
<td><xsl:value-of select="Capris"/></td>
</tr>
</xsl:for-each>
</table>
You had your xsl:for-each
matching rule wrong.您的
xsl:for-each
匹配规则错误。
Use the following template: it matches the root <Persons>
element, creates the <html>
and <body>
elements and the <table>
.使用以下模板:它匹配根
<Persons>
元素,创建<html>
和<body>
元素以及<table>
。
Finally, it iterates over the <Person>
elements with an xsl:for-each
.最后,它使用
xsl:for-each
遍历<Person>
元素。 This works, because the context node is right ( Person
is a child of Persons
).这是可行的,因为上下文节点是正确的(
Person
是Persons
的子节点)。
The Capris
situation is resolved by iterating over all Capris
elements and creating a <td>
for each present element. Capris
情况通过遍历所有Capris
元素并为每个当前元素创建一个<td>
来解决。 You can see this by looking at the borders of the elements.您可以通过查看元素的边界来看到这一点。
One thing left to be corrected is the xsl:sort
element.需要更正的一件事是
xsl:sort
元素。 It tries to sort by the value of a <Persib>
element which is not present in the example.它尝试按示例中不存在的
<Persib>
元素的值进行排序。 I guess that you can fix this on your own.我想你可以自己解决这个问题。
<xsl:template match="/Persons">
<html>
<body>
<table border="1">
<tr bgcolor="yellow">
<td><b>Shirts</b></td>
<td><b>Shorts</b></td>
<td><b>Capris</b></td>
<td><b>Capris</b></td>
<td><b>Capris</b></td>
</tr>
<xsl:for-each select="Person">
<xsl:sort select="Persib" />
<tr style="font-size: 10pt; font-family: verdana">
<td>
<xsl:value-of select="Shirts"/>
</td>
<td>
<xsl:value-of select="Pants/Jeans/Shorts"/>
</td>
<xsl:for-each select="Pants/Jeans/Capris">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
The output should be as desired. output 应符合要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.