我有一个订单行列表,每个产品都在它们上面。这些产品可能会形成一个自我引用的层次结构。我需要以这样一种方式排序:所有没有父母或父母在订单中缺失的产品位于顶部,其次是子女。最终结果中,没有一个孩子可能超过其父母。如何订购自引用xml
那么,如何可以订购以下XML:
<order>
<line><product code="3" parent="1"/></line>
<line><product code="2" parent="1"/></line>
<line><product code="6" parent="X"/></line>
<line><product code="1" /></line>
<line><product code="4" parent="2"/></line>
</order>
进入这个:
<order>
<line><product code="6" parent="X"/></line>
<line><product code="1" /></line>
<line><product code="2" parent="1"/></line>
<line><product code="3" parent="1"/></line>
<line><product code="4" parent="2"/></line>
</order>
注意,特定等级内的顺序并不重要,只要孩子节点如下的它是父母之后的一点。
我有一个工程层次的解决方案不超过预定深度:
<order>
<xsl:variable name="level-0"
select="/order/line[ not(product/@parent=../line/product/@code) ]"/>
<xsl:for-each select="$level-0">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:variable name="level-1"
select="/order/line[ product/@parent=$level-0/product/@code ]"/>
<xsl:for-each select="$level-1">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:variable name="level-2"
select="/order/line[ product/@parent=$level-1/product/@code ]"/>
<xsl:for-each select="$level-2">
<xsl:copy-of select="."/>
</xsl:for-each>
</order>
以上示例XSLT将为层次工作,3个级别的最大深度,并很容易扩展到更多,但我怎么能概括这一点,并让xslt正确排序任意级别的深度?
“*所有没有父母或父母在订单中缺失的产品都位于顶部,其次是子女。*”您是否想让父母立即跟随**,孩子,或一起列出同一代的所有项目。 –