(Recursion-2) 所有深度层次元素自增并保持遗传路径

(Recursion-2) Auto-incrementation for all depth level elements and keeping heredity path

注意这是另外两个问题的延续: and

但是,在问题的陈述中,问题与以下内容相同:

因此当前问题的措辞保持不变,只是与不同的新 XSLT 代码的实际情况有关。

1-来源

<root>
  <object id="a" id-3="COMMON-ID-1"/>
  <object id="b" id-3="COMMON-ID-2"/>
  <object id="c" id-3="COMMON-ID-3"/>

  <object id="aa" parent-id="a" id-3="value"/>
  <object id="bb" parent-id="b" id-3="value"/>
  <object id="cc" parent-id="c" id-3="value"/>


  <object id="aaa" parent-id="aa" id-3="value"/>
  <object id="aaaa" parent-id="aa" id-3="value"/>
  <object id="bbb" parent-id="bb" id-3="value"/>
  <object id="ccc" parent-id="cc" id-3="value"/>
  <object id="bbbb" parent-id="bbb" id-3="value"/>
  <object id="cccc" parent-id="ccc" id-3="value"/>
  <object id="bbbbb" parent-id="bbbb" id-3="value"/>
</root>

2-XSLT

<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 -->
    <root>
      <xsl:for-each select="exsl:node-set($chains)/object">
        <xsl:sort select="count(descendant::object)" data-type="number" order="descending"/>
        <xsl:if test="position()">
          <xsl:copy-of select="."/>
        </xsl:if>
      </xsl:for-each>
    </root>
  </xsl:template>

  <xsl:template match="object">
    <xsl:param name="common-id" select="@id-3"/>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="COMMON-ID">
        <xsl:value-of select="$common-id"/>
      </xsl:attribute>
    </xsl:copy>
    <xsl:apply-templates select="key('child', @id)">
      <xsl:with-param name="common-id" select="$common-id"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

3-输出

<?xml version="1.0" encoding="utf-16"?>
<root>
  <object id="a" id-3="COMMON-ID-1" STATUS="0" COMMON-ID="COMMON-ID-1" path="1"/>
  <object id="aa" parent-id="a" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-1" path="1/1"/>
  <object id="aaa" parent-id="aa" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-1" path="1/1/1"/>

  <object id="b" id-3="COMMON-ID-2" COMMON-ID="COMMON-ID-2" path="2"/>
  <object id="bb" parent-id="b" id-3="value" COMMON-ID="COMMON-ID-2" path="2/1"/>
  <object id="bbb" parent-id="bb" id-3="value" COMMON-ID="COMMON-ID-2" path="2/1/1"/>
  <object id="bbbb" parent-id="bbb" id-3="value" COMMON-ID="COMMON-ID-2" path="2/1/1/1"/>
  <object id="bbbbb" parent-id="bbbb" id-3="value" COMMON-ID="COMMON-ID-2" path="2/1/1/1/1"/>

  <object id="c" id-3="COMMON-ID-3" COMMON-ID="COMMON-ID-3" path="3"/>
  <object id="cc" parent-id="c" id-3="value" COMMON-ID="COMMON-ID-3" path="3/1"/>
  <object id="ccc" parent-id="cc" id-3="value" COMMON-ID="COMMON-ID-3" path="3/1/1"/>
  <object id="cccc" parent-id="ccc" id-3="value" COMMON-ID="COMMON-ID-3" path="3/1/1/1"/>
</root> 

我相信这会产生非常接近您想要的输出:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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">
    <root>
        <xsl:apply-templates select="object[not(@parent-id)]"/>
    </root>
</xsl:template>

<xsl:template match="object">
    <xsl:param name="common-id" select="@id-3"/>
    <xsl:param name="path"/>
    <xsl:variable name="new-path" select="concat($path, '/', position())"/>
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:attribute name="COMMON-ID">
            <xsl:value-of select="$common-id"/>
        </xsl:attribute>
        <xsl:attribute name="path">
            <xsl:value-of select="$new-path"/>
        </xsl:attribute>
    </xsl:copy>
    <xsl:apply-templates select="key('child', @id)">
        <xsl:with-param name="common-id" select="$common-id"/>
        <xsl:with-param name="path" select="$new-path"/>
   </xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>

AFAICT,唯一的区别是所有路径都以 / 字符开头。如果这是个问题,您可以这样做:

    <xsl:attribute name="path">
        <xsl:value-of select="substring($new-path, 2)"/>
    </xsl:attribute>

相反。