我想编写一个转换,它应该给出不同的父节点和不同的子节点。
在给定的示例中,我期望 Spocs 和地点集合不同。但是请您提供一个解决方案:
输入:
<DataCollection>
<Data>
<Item>Item1</Item>
<Price>6</Price>
<Area>Area1</Area>
<Contact>P1</Contact>
</Data>
<Data>
<Item>Item1</Item>
<Price>6.5</Price>
<Area>Area2</Area>
<Contact>P1</Contact>
</Data>
<Data>
<Item>Item1</Item>
<Price>6</Price>
<Area>Area1</Area>
<Contact>P2</Contact>
</Data>
<Data>
<Item>Item2</Item>
<Price>6</Price>
<Area>Area3</Area>
<Contact>P1</Contact>
</Data>
<Data>
<Item>Item2</Item>
<Price>6</Price>
<Area>Area1</Area>
<Contact>P2</Contact>
</Data>
<Data>
<Item>Item2</Item>
<Price>6</Price>
<Area>Area2</Area>
<Contact>P2</Contact>
</Data>
</DataCollection>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<MainTable1>
<Record>
<ItemNumber>Item1</ItemNumber>
<Rate>6</Rate>
<PlaceCollcection>
<Place>Area1</Place>
<Place>Area2</Place>
</PlaceCollcection>
<Spocs>
<Spoc>P1</Spoc>
<Spoc>P2</Spoc>
</Spocs>
</Record>
<Record>
<ItemNumber>Item2</ItemNumber>
<Rate>6</Rate>
<PlaceCollcection>
<Place>Area3</Place>
<Place>Area1</Place>
<Place>Area2</Place>
</PlaceCollcection>
<Spocs>
<Spoc>P1</Spoc>
<Spoc>P2</Spoc>
</Spocs>
</Record>
我使用的 Xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="ItemKey" match="Item" use="."/>
<xsl:template match="/DataCollection">
<MainTable1>
<xsl:apply-templates select="Data/Item[generate-id() =
generate-id(key('ItemKey',.)1])]"/>
</MainTable1>
</xsl:template>
<xsl:template match="Item">
<xsl:variable name="currentGroup" select="."/>
<Record>
<ItemNumber>
<xsl:value-of select="../Item"/>
</ItemNumber>
<Rate>
<xsl:value-of select="../Price"/>
</Rate>
<PlaceCollcection>
<xsl:for-each select="key('ItemKey', $currentGroup)">
<Place>
<xsl:value-of select="../Area"/>
</Place>
</xsl:for-each>
</PlaceCollcection>
<Spocs>
<xsl:for-each select="key('ItemKey', $currentGroup)">
<Spoc>
<xsl:value-of select="../Contact"/>
</Spoc>
</xsl:for-each>
</Spocs>
</Record>
</xsl:template>
</xsl:stylesheet>
请帮助区分 Place collection 和 Spoc