xslt将相同的节点分组到父节点下以及子节点中
我想在父标记(xslt将相同的节点分组到父节点下以及子节点中,xslt,Xslt,我想在父标记()和子标记()中对具有相同名称的节点进行分组 标记可能包含许多子标记,但这些子标记不应分组,而是每个Sku和项目中的元素应单独分组 我有一个输入xml文件,如下所示: <Products> <Item> <Dimensions> <Height>10</Height> </Dimensions> <Dimensions> <Weight>
)和子标记(
)中对具有相同名称的节点进行分组
标记可能包含许多
子标记,但这些子标记不应分组,而是每个Sku
和项目中的元素应单独分组
我有一个输入xml文件,如下所示:
<Products>
<Item>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
<Sku>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
</Sku>
<Sku>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
</Sku>
</Item>
</Products>
<Products>
<Item>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
</Item>
</Products>
10
10
橙色
蓝色
10
10
橙色
蓝色
10
10
橙色
蓝色
预期输出如下所示:
<Products>
<Item>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
<Sku>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
</Sku>
<Sku>
<Dimensions>
<Height>10</Height>
</Dimensions>
<Dimensions>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
</Color>
<Color>
<Attribute>Blue</Attribute>
</Color>
</Sku>
</Item>
</Products>
<Products>
<Item>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
</Item>
</Products>
10
10
橙色
蓝色
10
10
橙色
蓝色
10
10
橙色
蓝色
任何帮助都将不胜感激。
我使用了下面的xslt进行转换,但它只是在“Item”下挖掘元素
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="elements" match="Item/*[not(self::Sku)]" use="concat(name(), '|', generate-id(..))"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:for-each select="*[generate-id() = generate-id(key('elements', concat(name(), '|', generate-id(..)))[1])]">
<xsl:copy>
<xsl:apply-templates select="key('elements', concat(name(), '|', generate-id(..)))/*"/>
</xsl:copy>
</xsl:for-each>
<xsl:apply-templates select="Item" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您可以在两个分组之间共享相同的逻辑,如下所示:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="elements" match="*" use="concat(name(), '|', generate-id(..))"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="GroupChildren">
<xsl:param name="elements" select="*" />
<xsl:for-each select="$elements[generate-id() =
generate-id(key('elements',
concat(name(), '|',
generate-id(..))
)[1])]">
<xsl:copy>
<xsl:apply-templates select="key('elements',
concat(name(), '|', generate-id(..)))/*"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
<xsl:template match="Item">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:call-template name="GroupChildren">
<xsl:with-param name="elements" select="*[not(self::Sku)]" />
</xsl:call-template>
<xsl:apply-templates select="Sku" />
</xsl:copy>
</xsl:template>
<xsl:template match="Sku">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:call-template name="GroupChildren" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在示例输入上运行时,结果是:
<Products>
<Item>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
<Sku>
<Dimensions>
<Height>10</Height>
<Weight>10</Weight>
</Dimensions>
<Color>
<Attribute>Orange</Attribute>
<Attribute>Blue</Attribute>
</Color>
</Sku>
</Item>
</Products>
10
10
橙色
蓝色
10
10
橙色
蓝色
10
10
橙色
蓝色