注意:这是另外两个问题的延续:和
但是,在问题的说明中,这个问题与以下问题相同:
因此,本问题的措辞保持不变,只是针对不同的新XSLT代码的实际情况。
上有兄弟姐妹,则它看起来类似于"1/1/1“或"1/1/2”。
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> 发布于 2020-04-08 17:30:54
我相信这将产生一个非常接近你想要的产出:
XSLT1.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>而不是。
https://stackoverflow.com/questions/61102957
复制相似问题