Как @ michael.hor257k сказал, концептуально этот вопрос так же, как ваш предыдущий вопрос. Я думаю, вы просто не понимаете концепцию. Надеюсь, что комментарии в моем примере помогут.
Если я правильно понял вопрос, вы хотите вложить order
элементов в их «родительский» элемент order
на основе ParentID
. Так как мы основывая его на ParentID, это то, что мы будем использовать для нашего ключа ...
XML Input
<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 –
В этом случае мы сравниваем заказы с идентификатором и родительским идентификатором, который отличаются «использование» в ключе @ michael.hor257k –
Я считаю, что это то же самое. –