用一个最深的循环限制递归并为所有元素分配精确的 id

Limit recursion with one deepest loop and assign exact id to all elements

需要对元素进行混合排序。使用递归方法完成排序。 必须满足代码中未实现的两个条件:

1-来源

<root>

  <object id-1="aaaa" parent-id="bbbb" id-3="COMMON-ID-1"  STATUS="0" add-1="value" add-2="value"/>
  <object id-1="1111" parent-id="2222" id-3="COMMON-ID-2"  STATUS="0" add-1="value" add-2="value"/>

  <object id-1="bbbb" parent-id="cccc" id-3="value"        STATUS="1" add-1="value" add-2="value"/>
  <object id-1="2222" parent-id="3333" id-3="value"        STATUS="1" add-1="value" add-2="value"/>
  <object id-1="cccc" parent-id="dddd" id-3="value"        STATUS="1" add-1="value" add-2="value"/>
  <object id-1="3333" parent-id="4444" id-3="value"        STATUS="1" add-1="value" add-2="value"/>
  <object id-1="dddd" parent-id="eeee" id-3="value"        STATUS="1" add-1="value" add-2="value"/>
  <object id-1="4444" parent-id="5555" id-3="value"        STATUS="1" add-1="value" add-2="value"/>

</root>

2- 目前的 XSLT (产生所有递归,而只需要一个,不分配公共 ID)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:exslt="http://exslt.org/common">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:key name="by-id" match="object" use="@id-1"/>

  <xsl:template match="object" mode="ancestors">
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:attribute name="COMMON-ID">
        <xsl:value-of select="@id-3[@STATUS='0']"/>
      </xsl:attribute>

      <xsl:apply-templates select="key('by-id', @parent-id)" mode="ancestors"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="root/object[@STATUS='1']" mode="ancestors"/>
    </root>
  </xsl:template>

</xsl:stylesheet>

3-输出

<root>

  <object id-1="bbbb" id-2="cccc" id-3="value" STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-1"/> <!-- chain 1-->
  <object id-1="cccc" id-2="dddd" id-3="value" STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-1"/>
  <object id-1="dddd" id-2="eeee" id-3="value" STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-1"/>

  <object id-1="2222" id-2="3333" id-3="value"  STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-2"/>  <!-- chain 1-->
  <object id-1="3333" id-2="4444" id-3="value"  STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-2"/>
  <object id-1="4444" id-2="5555" id-3="value"  STATUS="1" add-1="value" add-2="value" COMMON-ID="COMMON-ID-2"/>

</root>

这是对您的根模板的更改。您的其他模板保持不变。希望这会有所帮助。

  <xsl:template match="/">
    <xsl:variable name="chains">
      <xsl:apply-templates select="root/object[@STATUS='1']" mode="ancestors"/>
    </xsl:variable>

    <xsl:variable name="chainList" select="exslt:node-set($chains)"/>

    <xsl:variable name="objects">
      <xsl:for-each select="$chainList/object">
        <element>
          <position>
            <xsl:value-of select="position()"/>
          </position>
          <numberOfObjects>
            <xsl:value-of select="count(.//object)+1"/>
          </numberOfObjects>
        </element>       
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="objectList" select="exslt:node-set($objects)"/>

    <xsl:variable name="sortedObjects">
      <xsl:for-each select="$objectList/element">
        <xsl:sort select="numberOfObjects" order="descending" data-type="number"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="sortedObjectList" select="exslt:node-set($sortedObjects)"/>

    <xsl:variable name="maxObjects" select="$sortedObjectList/element[1]/numberOfObjects"/>

    <root>
      <xsl:for-each select="$objectList/element[numberOfObjects = $maxObjects]">
        <xsl:variable name="position" select="position"/>

        <xsl:copy-of select="$chainList/object[number($position)]"/>

      </xsl:for-each>
    </root>
  </xsl:template>

Only how to choose the longest one chain?

考虑以下大大简化的例子。

重要
在这个例子中,假设一个 parent object 最多可以有一个 child object。这允许我们从祖先 objects(objects 没有 parent)开始递归并向下工作。否则我们将不得不为每个叶子object(没有任何child object的object创建一个单独的链并从那里向上递归。

XML

<root>
    <object id="a"/>
    <object id="b"/>
    <object id="c"/>
    <object id="aa" parent-id="a"/>
    <object id="bb" parent-id="b"/>
    <object id="cc" parent-id="c"/>
    <object id="aaa" parent-id="aa"/>
    <object id="bbb" parent-id="bb"/>
    <object id="ccc" parent-id="cc"/>
    <object id="bbbb" parent-id="bbb"/>
    <object id="cccc" parent-id="ccc"/>
    <object id="bbbbb" parent-id="bbbb"/>
</root>

XSLT 1.0(+ node-set 函数)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="child" match="object" use="@parent-id" />

<xsl:template match="/root">
    <!-- generate chains -->
    <xsl:variable name="chains">
        <xsl:apply-templates select="object[not(@parent-id)]"/>
    </xsl:variable>
    <!-- find the longest chain -->
    <xsl:for-each select="exsl:node-set($chains)/object">
        <xsl:sort select="count(descendant::object)" data-type="number" order="descending"/>
        <xsl:if test="position() =1 ">
            <xsl:copy-of select="."/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:template match="object">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="key('child', @id)"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

第一次通过后,$chains 变量将包含:

<object id="a">
  <object id="aa" parent-id="a">
    <object id="aaa" parent-id="aa"/>
  </object>
</object>
<object id="b">
  <object id="bb" parent-id="b">
    <object id="bbb" parent-id="bb">
      <object id="bbbb" parent-id="bbb">
        <object id="bbbbb" parent-id="bbbb"/>
      </object>
    </object>
  </object>
</object>
<object id="c">
  <object id="cc" parent-id="c">
    <object id="ccc" parent-id="cc">
      <object id="cccc" parent-id="ccc"/>
    </object>
  </object>
</object>

根据链的长度(即后代 objects 的数量)对链进行排序并选择最长的链后,我们将得到:

结果

<?xml version="1.0" encoding="UTF-8"?>
<object id="b">
  <object id="bb" parent-id="b">
    <object id="bbb" parent-id="bb">
      <object id="bbbb" parent-id="bbb">
        <object id="bbbbb" parent-id="bbbb"/>
      </object>
    </object>
  </object>
</object>

提示:随着递归向下工作,使用模板参数将公共值从祖先传递给它的所有后代是非常容易的。