(递归)所有深度级别元素的自动递增并保持链式遗传路径
(Recursion) auto-incrementation for all depth level elements and keeping heredity path in chain
是否可以通过实现增量原则来制作唯一的id(这里作为属性"path"的值)?
- 对于 3 级深度递归,例如它看起来像“1/1/1”或“1/1/2”- 如果在第三层有兄弟
- 我承认,从 XSLT 实现的技术角度来看,在 +1 增量计数之前使用级别前缀会很有用。这种情况也是正确的。
最主要的 - 组装唯一 ID ("path")
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(没有制作"path")
<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>
是否可以通过实现增量原则来制作唯一的id(这里作为属性"path"的值)?
- 对于 3 级深度递归,例如它看起来像“1/1/1”或“1/1/2”- 如果在第三层有兄弟
- 我承认,从 XSLT 实现的技术角度来看,在 +1 增量计数之前使用级别前缀会很有用。这种情况也是正确的。 最主要的 - 组装唯一 ID ("path")
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(没有制作"path")
<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>