如何“通过标签递归折叠"? XSLT1选择一组(相邻)标签? [英] How "to fold recursively by a tag" a group of selected (neighbor) tags with XSLT1?

查看:81
本文介绍了如何“通过标签递归折叠"? XSLT1选择一组(相邻)标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个上评论了当前问题:现在更加复杂,因为需要重现.通过示例进行详细说明:

I was comment the present problem at this other one: the present is more complex because needs a recurrence. Detailing by example:

  <root>
    <c>cccc</c>
    <a gr="g1_1">aaaa</a>    <b gr="g1_1">1111</b>
    <a gr="g2_1" into="g1_1">bbbb</a>   <b gr="g2_1" into="g1_1">56565</b>
    <a gr="g3_1" into="g2_1">BB</a>   <b gr="g3_1" into="g2_1">55</b>
    <a gr="g1_2">xxxx</a>    <b gr="g1_2">2222</b>
    <a gr="g2_2" into="g1_2">wwww</a>   <b gr="g2_2" into="g1_2">3433</b>
  </root>

必须用fold标记括起来,导致(在XSLT之后):

that must be enclosed by fold tags, resulting (after XSLT) in:

  <root>
    <c>cccc</c>

    <fold>
      <a gr="g1_1">aaaa</a>    <b gr="g1_1">1111</b>
      <fold><a gr="g2_1" into="g1_1">bbbb</a>   
            <b gr="g2_1" into="g1_1">56565</b>
            <fold><a gr="g3_1" into="g2_1">BB</a>   
                  <b gr="g3_1" into="g2_1">55</b>
            </fold>
       </fold>
    </fold>

    <fold>
      <a gr="g1_2">xxxx</a>    <b gr="g1_2">2222</b>
      <fold><a gr="g2_2" into="g1_2">wwww</a>   
            <b gr="g2_2" into="g1_2">3433</b>
      </fold>
    </fold>
  </root>


注释

该示例具有用于分组的标签"(@gr)和用于超分组"的标签(@into指向父组).

The example have a "label for grouping" (@gr) and a label for "super-grouping" (@into pointing to the parent group).

@gr是唯一组的ID,并且还使用语法"g" level "_" level-id指示折叠级别",因此,如果需要,我们可以为折叠级别添加一个显式属性...或者可以添加和辅助结构(gdef作为输入元数据),

The @gr is an ID for unique groups, and also indicates the "folding level" with the syntax "g" level "_" level-id, so, if need we can add an explicit attribute for folding level... Or can add and auxiliar structure (gdef as input metadata),

 <gdef>
  <group gr="g1_1" level="1" into=""/>
  <group gr="g2_1" level="2" into="g1_1"/>
  <group gr="g3_1" level="3" into="g2_1"/>
  <group gr="g1_2" level="1" into=""/>
  <group gr="g2_2" level="2" into="1_2"/>
 </gdef>

或其他

推荐答案

您不需要递归.您只需要小心地将应用模板应用于兄弟姐妹即可.
下面的代码是对您其他(参考)问题的解决方案的改编.

You don't need recursion. You just need to careful apply-templates to siblings.
The code below is an adaptation from the solution in your other (referred) question.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="*" />
  <xsl:output indent="yes" />
  <xsl:key name="key" match="*[@gr]" use="@gr" />

  <xsl:template match="*[*/@gr]">
    <xsl:copy>
      <xsl:apply-templates select="*[not(@into)]"/><!--start by most top-level ones-->
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*[@gr]"/>
  <xsl:template match="*[@gr][generate-id() = generate-id(key('key', @gr)[1])]">
    <fold>
      <xsl:for-each select="key('key', @gr)">
        <xsl:call-template name="identity" />
      </xsl:for-each>
      <xsl:apply-templates select="following-sibling::*[@into = current()/@gr]"/>
    </fold>
  </xsl:template>
  <xsl:template match="node()|@*" name="identity" >
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

这篇关于如何“通过标签递归折叠"? XSLT1选择一组(相邻)标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆