Adding common attribute to all chains in recursion

放肆的年华 提交于 2021-02-11 14:53:15

问题


A follow-up question to: Limit recursion with one deepest loop and assign exact id to all elements

This code collects recursion. It is necessary that at the same time all recursion elements receive a COMMON-ID attribute with the value from the first element of the chain. This first element has also an attribue STATUS="0". Other elements have STATUS="1" (for solution it may be useful).

1-source

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

  <object id="aa" parent-id="a" id-3="value" STATUS="1"/>
  <object id="bb" parent-id="b" id-3="value" STATUS="1"/>
  <object id="cc" parent-id="c" id-3="value" STATUS="1"/>
  <object id="aaa" parent-id="aa" id-3="value" STATUS="1"/>
  <object id="bbb" parent-id="bb" id-3="value" STATUS="1"/>
  <object id="ccc" parent-id="cc" id-3="value" STATUS="1"/>
  <object id="bbbb" parent-id="bbb" id-3="value" STATUS="1"/>
  <object id="cccc" parent-id="ccc" id-3="value" STATUS="1"/>
  <object id="bbbbb" parent-id="bbbb" id-3="value" STATUS="1"/>
</root>

2-present XSLT (do not assign COMMON-ID)

<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 -->
    <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>
</xsl:template>

<xsl:template match="object">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="key('child', @id)"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

3-desired output

<?xml version="1.0" encoding="UTF-8"?>
<object id="b" id-3="COMMON-ID-1" STATUS="0" COMMON-ID="COMMON-ID-2">
  <object id="bb" parent-id="b" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-2>
    <object id="bbb" parent-id="bb" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-2>
      <object id="bbbb" parent-id="bbb" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-2>
        <object id="bbbbb" parent-id="bbbb" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-2/>
      </object>
    </object>
  </object>
</object>
<object id="c"  id-3="COMMON-ID-3" STATUS="0" COMMON-ID="COMMON-ID-3">
  <object id="cc" parent-id="c" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-3">
    <object id="ccc" parent-id="cc" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-3">
      <object id="cccc" parent-id="ccc" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-3"/>
    </object>
  </object>
</object>
<object id="a" id-3="COMMON-ID-1" STATUS="0" COMMON-ID="COMMON-ID-1">
  <object id="aa" parent-id="a" id-3="value" STATUS="1" COMMON-ID="COMMON-ID-1">
    <object id="aaa" parent-id="aa" id-3="COMMON-ID-1" STATUS="1" COMMON-ID="COMMON-ID-1"/>
  </object>
</object>

回答1:


Change your 2nd template to:

<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:apply-templates select="key('child', @id)">
            <xsl:with-param name="common-id" select="$common-id"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>


来源:https://stackoverflow.com/questions/60981902/adding-common-attribute-to-all-chains-in-recursion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!