【发布时间】:2012-11-14 10:46:14
【问题描述】:
我有一个复杂的 xml 结构,看起来像:
<Items>
<Item>
<ItemTexts>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
<ItemTextsLine>1</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
<ItemTextsLine>2</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
<ItemTextsLine>3</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
<ItemTextsLine>4</ItemTextsLine>
</ItemText>
</ItemTexts>
</Item>
<Item>
<ItemTexts>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
<ItemTextsLine>1</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
<ItemTextsLine>2</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
<ItemTextsLine>3</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
<ItemTextsLine>4</ItemTextsLine>
</ItemText>
</ItemTexts>
</Item>
<Item>
<ItemTexts>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
<ItemTextsLine>1</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type1</ItemTextsType>
<ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
<ItemTextsLine>2</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
<ItemTextsLine>3</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
<ItemTextsLine>4</ItemTextsLine>
</ItemText>
</ItemTexts>
</Item>
<Item>
<ItemTexts>
<ItemText>
<ItemTextsType>type3</ItemTextsType>
<ItemTextsTypeDesc>description31</ItemTextsTypeDesc>
<ItemTextsLine>1</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type3</ItemTextsType>
<ItemTextsTypeDesc>description32</ItemTextsTypeDesc>
<ItemTextsLine>2</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
<ItemTextsLine>3</ItemTextsLine>
</ItemText>
<ItemText>
<ItemTextsType>type2</ItemTextsType>
<ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
<ItemTextsLine>4</ItemTextsLine>
</ItemText>
</ItemTexts>
</Item>
</Items>
每个<Item> 都有更多的 xml 元素,但我现在不会指定它们。
我在每个项目上都使用 xsl 运行,例如:
<xsl:for-each select="Items/Item">
我为每个<Item> 和每行中的许多<td> 生成一个表格。
我需要一个示例,说明如何将 <ItemText> 按 <ItemTextsType> 分别为每个 <Item> 分组。
我会将结果安排在如下表格中:
<table>
<%--tr for each Item in Items--%>
<tr>
<td>
some value from xml element according <b> Item1 </b>
</td>
<td>
some more value from xml element according <b> Item1 </b>
</td>
<%--
.
.
.
--%>
<td>
<table width="100%" dir="ltr">
<tbody>
<tr style="background-color: #507CD1; text-align: center">
<td colspan="3" style="font: bold; color: white">
Item1
</td>
</tr>
<tr>
<td style="height: 35px; font: bold; color: #507CD1;">
type1
</td>
</tr>
<tr>
<td>
description11
</td>
</tr>
<tr>
<td>
description12
</td>
</tr>
<tr>
<td style="height: 35px; font: bold; color: #507CD1;">
type2
</td>
</tr>
<tr>
<td>
description21
</td>
</tr>
<tr>
<td>
description22
</td>
</tr>
</tbody>
</table>
</td>
<td>
even more value from xml element according <b> Item1 </b>
</td>
<%--
.
.
.
--%>
</tr>
<tr>
<td>
some value from xml element according <b> Item2 </b>
</td>
<td>
some more value from xml element according <b> Item2 </b>
</td>
<%--
.
.
.
--%>
<td>
<table width="100%" dir="ltr">
<tbody>
<tr style="background-color: #507CD1; text-align: center">
<td colspan="3" style="font: bold; color: white">
Item2
</td>
</tr>
<tr>
<td style="height: 35px; font: bold; color: #507CD1;">
type1
</td>
</tr>
<tr>
<td>
description11
</td>
</tr>
<tr>
<td>
description12
</td>
</tr>
<tr>
<td style="height: 35px; font: bold; color: #507CD1;">
type2
</td>
</tr>
<tr>
<td>
description21
</td>
</tr>
<tr>
<td>
description22
</td>
</tr>
</tbody>
</table>
</td>
<td>
even more value from according <b> Item2 </b>
</td>
<%--
.
.
.
--%>
</tr>
<%-- and so on--%>
</table>
关键是每个<Item> 都有单独的分组到它自己的<tr><td><table> 里面<table> 有<ItemTextsType> 分组
我尝试过类似的东西
<xsl:key name="item-texts-type" match="ItemText" use="ItemTextsType" />
<xsl:for-each select="ItemTexts/ItemText[count(. | key('item-texts-type', ItemTextsType)[1]) = 1]">
<xsl:sort select="ItemTextsType" />
<tr>
<td style ="height:35px;font: bold; color:#507CD1;">
<xsl:value-of select="ItemTextsType" />
</td>
</tr>
<xsl:for-each select="key('item-texts-type', ItemTextsType)">
<tr>
<td>
<xsl:value-of select="ItemTextsTypeDesc" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
但它只适用于非重复节点(如果只有一个<Item>它会正常工作)。
我无法更改 xml,因为它来自客户。
请帮帮我,我需要它尽快。
谢谢!!!!
【问题讨论】:
-
这和你的previous seemingly identical question有什么区别?在我回答这个问题之前我没有注意到那个问题,而且我似乎独立地提出了与之前接受的答案相同的解决方案。
-
最初我给出了所需输出的示例,ABach 很友好地回答了它。但后来我意识到我需要的输出与我之前写的不同,所以我改变了它。然后我决定打开另一个问题,所以也许上一个问题会对某人有所帮助。我希望在编辑之前将旧问题返回到状态,但到目前为止我做不到 - 我是 Stackoverflow 的新手......
-
如果您转到原始问题的编辑历史记录,您应该会看到针对每个修订号的“回滚”选项,您可以使用该选项将问题回滚到该版本。
-
谢谢,编辑了问题,使其与答案相匹配。
标签: xml xslt xml-parsing xslt-1.0 xsl-grouping