(递归)所有深度级别元素的自动增量并保持链中的遗传路径 [英] (Recursion) auto-incrementation for all depth level elements and keeping heredity path in chain

查看:13
本文介绍了(递归)所有深度级别元素的自动增量并保持链中的遗传路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过实现增量原则来制作唯一的 id(这里作为属性路径"的值)?

Is it possible to make unique id (here as an attribute "path"'s value) by implementing incremental principle?

  • 对于 3 lvl 深度递归,例如它看起来像1/1/1"或1/1/2"——如果在第三层有兄弟姐妹
  • 我可能承认,从 XSLT 实现的技术角度来看,在 +1 增量计数之前使用级别前缀会很有用.这种情况也是正确的.主要的事情 - 组装唯一 ID(路径")

1 个来源

<root>

  <object id="a" id-3="value"/>
  <object id="b" id-3="value"/>
  <object id="c" id-3="value"/>

  <object id="aa" parent-id="a" id-3="value"/>
  <object id="aaa" parent-id="aa" id-3="value"/>
  <object id="aaaa" parent-id="aaa" id-3="value"/>

  <object id="bb" parent-id="b" id-3="value"/>
  <object id="bbb" parent-id="bb" id-3="value"/>
  <object id="bbbb-1" parent-id="bbb" id-3="value"/>    <!-- note - siblings-->
  <object id="bbbb-2" parent-id="bbb" id-3="value"/>    <!-- note - siblings-->

  <object id="cc" parent-id="c" id-3="value"/>
  <object id="ccc" parent-id="cc" id-3="value"/>
  <object id="cccc" parent-id="ccc" id-3="value"/>

</root>

2-XSLT(不制作路径")

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslt="http://exslt.org/common">
  <xsl:key name="object-by-id" match="object" use="@id"/>
  <xsl:key name="object-by-parent-id" match="object" use="string(@parent-id)"/>

  <xsl:variable name="fold-rtf">
    <xsl:apply-templates select="/" mode="fold"/>
  </xsl:variable>
  <xsl:variable name="folded-tree" select="exslt:node-set($fold-rtf)"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="object/@*[last()]">
    <xsl:variable name="current" select=".."/>
    <xsl:copy/>
    <xsl:for-each select="$folded-tree">
      <xsl:for-each select="key('object-by-id',$current/@id)">

        <!-- ============================= -->

        <xsl:for-each select="ancestor-or-self::*">
          <xsl:attribute name="path">
            <xsl:value-of select="position()"/>
          </xsl:attribute>
        </xsl:for-each>
        <!-- ============================= -->

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

  <xsl:template match="/|*" mode="fold">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="key('object-by-parent-id',string(@id))" mode="fold">
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

3-假设输出

<root>

  <object id="a" id-3="value" path="1"/>
  <object id="b" id-3="value" path="2"/>
  <object id="c" id-3="value" path="3"/>

  <object id="aa" parent-id="a" id-3="value"        path="1/1"/>
  <object id="aaa" parent-id="aa" id-3="value"      path="1/1/1"/>
  <object id="aaaa" parent-id="aa" id-3="value"     path="1/1/1/1"/>

  <object id="bb" parent-id="b" id-3="value"        path="2/1"/>
  <object id="bbb" parent-id="bb" id-3="value"      path="2/1/1"/>
  <object id="bbbb-1" parent-id="bbb" id-3="value"  path="2/1/1/1"/>    <!-- note - siblings-->
  <object id="bbbb-2" parent-id="bbb" id-3="value"  path="2/1/1/2"/>    <!-- note - siblings-->

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

</root>

推荐答案

我觉得你可以用 xsl:number:

  <xsl:template match="object/@*[last()]">
    <xsl:variable name="current" select=".."/>
    <xsl:copy/>
    <xsl:for-each select="$folded-tree">
      <xsl:for-each select="key('object-by-id',$current/@id)">


        <xsl:attribute name="path">
            <xsl:number level="multiple" format="1/1"/>
        </xsl:attribute>

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

https://xsltfiddle.liberty-development.net/pNmC4HV

这篇关于(递归)所有深度级别元素的自动增量并保持链中的遗传路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆