像@ michael.hor257k说的,概念上这个问题和你以前的问题是一样的。我想你只是不理解这个概念。希望我的例子中的评论会有所帮助。
如果我正确地理解了这个问题,您想根据ParentID
将order
元素嵌套在它们的“父”order
元素中。由于我们是基于此的PARENTID,这就是我们将使用我们的关键...
XML输入
<main>
<order>
<ID>123</ID>
<Name>ABC</Name>
</order>
<order>
<ID>4556</ID>
<Name>AAA</Name>
<ParentID>123</ParentID>
</order>
</main>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Create a key containing order elements that contain a non-empty ParentID.-->
<xsl:key name="orderByParentId" match="order[string(ParentID)]" use="ParentID"/>
<!--Identity transform (https://www.w3.org/TR/xslt#copying)-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/main">
<xsl:copy>
<!--To get the high level order elements, only apply-templates when order
does not contain a non-empty ParentID child.-->
<xsl:apply-templates
select="@*|order[not(string(ParentID))]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order">
<xsl:copy>
<!--Don't apply-templates to ParentID; we don't want to keep those.
Do apply-templates to the key 'orderByParentId' when the key matches
the current order's ID child.-->
<xsl:apply-templates
select="@*|*[not(self::ParentID)]|key('orderByParentId',ID)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<main>
<order>
<ID>123</ID>
<Name>ABC</Name>
<order>
<ID>4556</ID>
<Name>AAA</Name>
</order>
</order>
</main>
我不明白这与你的其他问题有什么不同:http://stackoverflow.com/questions/40428756/combining-xml-xpath-or-xquery –
在这种情况下,wee是比较订单与ID和父母的ID是不同的“使用”在钥匙@ michael.hor257k –
我相信这是一回事。 –