以这种格式使用 XML: ?xml version="1.0"?GetResult version="1.0"Fetch StartTime2004-08-01 00:00:00/StartTime EndTime2004-08-01 00:00:00/EndTime/FetchItems Item NameItem Name Number 1/Name Data Datum Timestamp2004-07-31 16:00:00+00:00
<?xml version="1.0"?>
<GetResult version="1.0">
<Fetch>
<StartTime>2004-08-01 00:00:00</StartTime>
<EndTime>2004-08-01 00:00:00</EndTime>
</Fetch>
<Items>
<Item>
<Name>Item Name Number 1</Name>
<Data>
<Datum>
<Timestamp>2004-07-31 16:00:00+00:00</Timestamp>
<Value><![CDATA[25]]></Value>
</Datum>
<Datum>
<Timestamp>2004-07-31 18:00:00+00:00</Timestamp>
<Value><![CDATA[35]]></Value>
</Datum>
</Data>
</Item>
<Item>
<Name>Item Number 2</Name>
<Data>
<Datum>
<Timestamp>2004-07-31 16:00:00+00:00</Timestamp>
<Value><![CDATA[45]]></Value>
</Datum>
<Datum>
<Timestamp>2004-07-31 17:00:00+00:00</Timestamp>
<Value><![CDATA[55]]></Value>
</Datum>
<Datum>
<Timestamp>2004-07-31 18:00:00+00:00</Timestamp>
<Value><![CDATA[65]]></Value>
</Datum>
</Data>
</Item>
</Items>
</GetResult>
我希望能够使用XSLT生成这样的表:
<table>
<tr>
<th>Timestamp</th>
<th>Item Name Number 1</th>
<th>Item Number 2</th>
</tr>
<tr>
<td>2004-07-31 16:00:00+00:00</td>
<td>25</td>
<td>45</td>
</tr>
<tr>
<td>2004-07-31 17:00:00+00:00</td>
<td></td>
<td>55</td>
</tr>
<tr>
<td>2004-07-31 18:00:00+00:00</td>
<td>35</td>
<td>65</td>
</tr>
</table>
无论返回多少项目以及每个项目下的基准面数量,这都必须有效.我已经阅读了其他类似的答案,没有任何运气.我是XSLT的新手,它让我发疯.对此的解决方案将不胜感激.
这是一个利用Muenchian Grouping相当可怕的方法的方法,如果你看一下StackOverflow中的其他XSLT问题,你可能会看到这个方法,所以值得了解.在这种情况下,Muenchian Grouping将用于循环遍历distist Timestamp元素.首先,定义一个键来查找时间戳元素
<xsl:key name="Timestamps" match="Timestamp" use="."/>
因此,如果您使用它来查找’2004-07-31 16:00:00 00:00’的键,它将包含两个Timestamp元素,但是’2004-07-31 17:00:00 00: 00’只包含一个.
要遍历不同的Timestamp元素,首先要遍历所有Timestamp元素,就像这样
<xsl:for-each select="//Timestamp">
但是,您需要一个XSL:IF条件来检查Timestamp元素是否是该值的第一个出现.这是通过使用密钥完成的.如果元素恰好是键列表中的第一个元素,则可以对其进行处理.
<xsl:if test="generate-id(.) = generate-id(key('Timestamps',.)[1])">
generate-id是要在测试时使用的方法,两个元素是相同的.完全放在一起给出:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Timestamps" match="Timestamp" use="."/>
<xsl:template match="/">
<table>
<tr>
<th>Timestamp</th>
<!-- Output the Item headers -->
<xsl:for-each select="//Item">
<th>
<xsl:value-of select="Name"/>
</th>
</xsl:for-each>
</tr>
<!-- Loop through all Timestamps -->
<xsl:for-each select="//Timestamp">
<xsl:sort select="."/>
<!-- Only process the element if it is the first occurence of this value -->
<xsl:if test="generate-id(.) = generate-id(key('Timestamps',.)[1])">
<xsl:variable name="Timestamp" select="."/>
<tr>
<td>
<xsl:value-of select="."/>
</td>
<xsl:for-each select="//Item">
<td>
<!-- Output the relevant Value for the Item -->
<xsl:value-of select="Data/Datum[Timestamp=$Timestamp][1]/Value"/>
</td>
</xsl:for-each>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
