问题
Is it possible to make unique id (here as an attribute "path"'s value) by implementing incremental principle?
- for 3 lvl depth recursion, for example it would look like "1/1/1" or "1/1/2" - if there is a sibling on a third level
- I may admit that from technical point of XSLT realization it would be useful to use level prefix before +1 increment counting. That case also would be correct. the main thing - to assembly unique id ("path")
1-source
<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 (without making "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-assumed output
<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>
回答1:
I think you can use 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
来源:https://stackoverflow.com/questions/61076622/recursion-auto-incrementation-for-all-depth-level-elements-and-keeping-heredit