Xml 递归:按秩集成复杂条件和顺序

Xml 递归:按秩集成复杂条件和顺序,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,现有XSLT代码需要通过以下条件进行增强 1-与递归算法一起添加检查,以确保每个比较行(元素)具有: 属性状态为0 属性status2为null(这里表示元素中实际上没有属性status2) (仅当这两个条件为真时才能创建链) 2-排名顺序。所有还原(或还原)的链最终必须按“rank”属性排序 (它可能与本机递归匹配,但有时不匹配。无论如何,“rank”属性具有更高的优先级。 秩没有稳定的命名法(如1-2-3-4),但其链可以通过比较“更多”-“更少”来计算。(1

现有XSLT代码需要通过以下条件进行增强

1-与递归算法一起添加检查,以确保每个比较行(元素)具有:

  • 属性状态为0
  • 属性status2为null(这里表示元素中实际上没有属性status2) (仅当这两个条件为真时才能创建链)
2-排名顺序。所有还原(或还原)的链最终必须按“rank”属性排序 (它可能与本机递归匹配,但有时不匹配。无论如何,“rank”属性具有更高的优先级。 秩没有稳定的命名法(如1-2-3-4),但其链可以通过比较“更多”-“更少”来计算。(1<3<7)

源代码

<A>
  <X id="top"                         text="first"  text2="*"     status="0" rank="1"/>
  <X id="middle"  id-parent="top"     text="second" text2="**"    status="0" rank="3"/>
  <X id="bottom"  id-parent="middle"  text="third"  text2="***"   status="0" rank="6"/>

  <X id="bottom2" id-parent="middle"  text="fourth" text2="****"  status="0" rank="2"/> <!--note rank! bottom and middle should be switching because "bottom2 has a "higher" rank-->
  <X id="bottom3" id-parent="middle"  text="fifth"  text2="*****" status="2" rank="6" status2="any-value-make-its-status-not_null"/>
</A>

互联

所需输出

<?xml version="1.0" encoding="UTF-8"?><A>
  <X id="top" text="first" text2="*" chain-text="first *" level-1="first | *"/>
  <X id="middle" id-parent="top" text="second" text2="**" chain-text="first * | second **" level-1="first | *" level-2="second | **"/>
  <X id="bottom" id-parent="middle" text="third" text2="***" chain-text="first * | second ** | third ***" level-1="first | *" level-2="second | **" level-3="third | ***"/>

  <!--new conditions are clearly visible on the following lines-->
  <X id="bottom2" id-parent="middle"  text="fourth" text2="****" chain-text="first * | fourth **** | second **" level-1="first | *" level-2="fourth | ****" level-3="second | **"/> <!-- bottom and middle are switching because of 'rank' -->
  <X id="bottom3" id-parent="middle"  text="fifth"  text2="*****" status="1" rank="6" status2="any-value-here-make-its-status-not_null"/> <!--nothing happens with this line because of 'status' and 'status2' attributes  -->
</A>

您的措辞掩盖了问题。您需要折叠层次结构(用
@id
@id parent
属性表示),然后按
@rank
属性对路径进行排序

此XSLT 1.0样式表(带有EXSLT扩展):


| 
产出:

<A>
  <X id="top" text="first" text2="*" status="0" rank="1" 
     chain-text="first *" level-1="first | *" />
  <X id="middle" id-parent="top" text="second" text2="**" status="0" rank="3" 
     chain-text="first * | second **" level-1="first | *" level-2="second | **" />
  <X id="bottom" id-parent="middle" text="third" text2="***" status="0" rank="6" 
     chain-text="first * | second ** | third ***" level-1="first | *" level-2="second | **" level-3="third | ***" />

  <X id="bottom2" id-parent="middle" text="fourth" text2="****" status="0" rank="2" 
     chain-text="first * | fourth **** | second **" level-1="first | *" level-2="fourth | ****" level-3="second | **" /> <!--note rank! bottom and middle should be switching because "bottom2 has a "higher" rank-->
  <X id="bottom3" id-parent="middle" text="fifth" text2="*****" status="2" rank="6" status2="any-value-make-its-status-not_null" />
</A>

注意:使用
node-set()
函数进行两步转换,更改另一个输入文档使用
key()
函数的上下文,强制执行键的字符串转换,因为有关于节点集作为键的特殊规则,而且我想使用空字符串键


建议:所有这些都可以转化为几行XSLT 2.0+

感谢您的代码和建议!我会仔细检查这两行代码。在您的代码中添加了一个模式。它删除了所有不构成递归链的节点。但这里有一个关于优化的问题。也许您对此有自己的看法。在我看来,其中一个选择是在不使用DELETE模式的情况下解决问题,并且最初只输出形成链的节点。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exslt="http://exslt.org/common">
    <xsl:key name="X-By-id" match="X" use="@id"/>
    <xsl:key name="X-By-parent-id" match="X" use="string(@id-parent)"/>

    <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="X[@status=0][not(@status2)]/@*[last()]">
        <xsl:variable name="current" select=".."/>
        <xsl:copy/>
        <xsl:for-each select="$folded-tree">
            <xsl:for-each select="key('X-By-id',$current/@id)">
                <xsl:attribute name="chain-text">
                    <xsl:for-each select="ancestor-or-self::*">
                        <xsl:sort select="@rank"/>
                        <xsl:if test="position()!=1"> | </xsl:if>
                        <xsl:value-of select="concat(@text,' ',@text2)"/>
                    </xsl:for-each>                
                </xsl:attribute>
                <xsl:for-each select="ancestor-or-self::*">
                    <xsl:sort select="@rank"/>
                    <xsl:attribute name="level-{position()}">
                        <xsl:value-of select="concat(@text,' | ',@text2)"/>
                    </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('X-By-parent-id',string(@id))" mode="fold">
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<A>
  <X id="top" text="first" text2="*" status="0" rank="1" 
     chain-text="first *" level-1="first | *" />
  <X id="middle" id-parent="top" text="second" text2="**" status="0" rank="3" 
     chain-text="first * | second **" level-1="first | *" level-2="second | **" />
  <X id="bottom" id-parent="middle" text="third" text2="***" status="0" rank="6" 
     chain-text="first * | second ** | third ***" level-1="first | *" level-2="second | **" level-3="third | ***" />

  <X id="bottom2" id-parent="middle" text="fourth" text2="****" status="0" rank="2" 
     chain-text="first * | fourth **** | second **" level-1="first | *" level-2="fourth | ****" level-3="second | **" /> <!--note rank! bottom and middle should be switching because "bottom2 has a "higher" rank-->
  <X id="bottom3" id-parent="middle" text="fifth" text2="*****" status="2" rank="6" status2="any-value-make-its-status-not_null" />
</A>