我无法创建XSLT以执行我认为应该直截了当的事情......
给出以下XML:
<DATA_DS>
<LIST_ITEMS>
<ITEMS>
<abc>2011</abc>
<def>ABC Company</def>
<value>23</value>
</ITEMS>
<ITEMS>
<abc>2011</abc>
<def>ABC Company 1</def>
<value>11</value>
</ITEMS>
<ITEMS>
<abc>2010</abc>
<def>ABC Company 2</def>
<value>15</value>
</ITEMS>
<ITEMS>
<abc>2010</abc>
<def>ABC Company 3</def>
<value>6</value>
</ITEMS>
<ITEMS>
<abc>2010</abc>
<def>ABC Company 4</def>
<value>44</value>
</ITEMS>
</LIST_ITEMS></DATA_DS>
我将如何使用XSLT将其转换为以下内容:
<table>
<row>23</row>
<row>11</row>
</table>
<table>
<row>15</row>
<row>6</row>
<row>44</row>
</table>
这个想法是每次遇到新的“abc”时创建一个新的“表”
答案 0 :(得分:1)
在xslt 2.0指令for-each-group
中,您正在寻找。
关注样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<output>
<xsl:for-each-group select="DATA_DS/LIST_ITEMS/ITEMS" group-by="abc">
<table>
<xsl:for-each select="current-group()">
<row>
<xsl:value-of select="current()/value" />
</row>
</xsl:for-each>
</table>
</xsl:for-each-group>
</output>
</xsl:template>
</xsl:stylesheet>
产生输出
<?xml version="1.0" encoding="UTF-8"?>
<output>
<table>
<row>23</row>
<row>11</row>
</table>
<table>
<row>15</row>
<row>6</row>
<row>44</row>
</table>
</output>