我发现XSLT 1.0 Meunchian分组非常复杂。如果您可以使用xsl:key
引导我进行以下操作,那就太好了。实际的XML很大,我只使用其中的一部分来指示结构。
如果您需要任何其他澄清,请告诉我。
我的要求是以表格格式显示以下示例XML,首先按ItemType名称分组,然后按客户名称进行分组。请注意,节点Details
总是只有一个节点Detail
所需的输出
itemType客户名称价格
书约翰·史密斯7
DVD John Smith 45
DVD Jane Doe 44
输入:
<Item>
<SomeRandomField>abc</SomeRandomField>
<Details>
<Detail>
<Price>7.00</Price
<CustomerName>John Smith</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>1</Key>
<Name>Book</Name>
</ItemType>
</Item>
<Item>
<SomeRandomField>mno</SomeRandomField>
<Details>
<Detail>
<Price>45.00</Price
<CustomerName>John Smith</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>2</Key>
<Name>DVD</Name>
</ItemType>
</Item>
<Item>
<SomeRandomField>xyz</SomeRandomField>
<Details>
<Detail>
<Price>44.00</Price
<CustomerName>Jane Doe</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>2</Key>
<Name>DVD</Name>
</ItemType>
</Item>
您可以使用以下XSLT来转换XML以获取所需的输出,并且我通过添加<Items>
root Node修改了XML。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="Items">
<table>
<tr>
<td>
<b>ItemType</b>
</td>
<td>
<b>Customer Name</b>
</td>
<td>
<b>Price</b>
</td>
</tr>
<xsl:for-each select="Item">
<xsl:sort select="ItemType/Name" order="ascending"/>
<tr>
<td>
<xsl:value-of select="ItemType/Name"/>
</td>
<td>
<xsl:value-of select="Details/Detail/CustomerName"/>
</td>
<td>
<xsl:value-of select="Details/Detail/Price"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
我相信以下是所需的结果。该密钥用于按ItemType进行分组,然后您可以在需要时包括一个排序。
。xml
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<SomeRandomField>abc</SomeRandomField>
<Details>
<Detail>
<Price>7.00</Price>
<CustomerName>John Smith</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>1</Key>
<Name>Book</Name>
</ItemType>
</Item>
<Item>
<SomeRandomField>mno</SomeRandomField>
<Details>
<Detail>
<Price>45.00</Price>
<CustomerName>John Smith</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>2</Key>
<Name>DVD</Name>
</ItemType>
</Item>
<Item>
<SomeRandomField>xyz</SomeRandomField>
<Details>
<Detail>
<Price>44.00</Price>
<CustomerName>Jane Doe</CustomerName>
</Detail>
</Details>
<ItemType>
<Key>2</Key>
<Name>DVD</Name>
</ItemType>
</Item>
</Items>
xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="itemname" match="/Items/Item/ItemType" use="Name"/>
<xsl:key name="custname" match="/Items/Item/Details/Detail" use="CustomerName"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="/Items/Item/ItemType
[generate-id(.) = generate-id(key('itemname', Name))]
">
<xsl:variable name="local_item" select="Name"/>
<xsl:for-each select="/Items/Item">
<xsl:if test="ItemType/Name = $local_item">
<p><xsl:value-of select="$local_item"/>-
<xsl:value-of select="Details/Detail/CustomerName"/>-
<xsl:value-of select="Details/Detail/Price"/></p>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
html
<html>
<body>
<p>Book-John Smith-7.00</p>
<p>DVD-John Smith-45.00</p>
<p>DVD-Jane Doe-44.00</p>
</body>
</html>
除了上面的答案外,我相信这个示例也很简单。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="itemname" match="/Items/Item/ItemType" use="Name"/>
<xsl:key name="custname" match="/Items/Item/Details/Detail" use="CustomerName"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="/Items/Item">
<xsl:sort select="ItemType/Name"/>
<xsl:sort select="Details/Detail/CustomerName"/>
<xsl:value-of select="."/><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>