使用 XSLT 拆分和展平节点

split and flatten nodes with XSLT

我不能有任何嵌套跨度,所以我需要将它们展平并连接它们的 class 属性,以便我可以跟踪哪些 class 是父级。

这是一个简化的输入

<body>
    <h1 class="section">Title</h1>
    <p class="main">
        ZZZ
        <span class="a">
            AAA
            <span class="b">
                BBB
                <span class="c">
                    CCC
                    <preserveMe>
                        eeee
                    </preserveMe>
                </span>
                bbb
                <preserveMe>
                    eeee
                </preserveMe>
            </span>
            aaa
        </span>
    </p>
</body>

这是期望的输出

<body>
    <h1 class="section">Title</h1>
    <p class="main">
        ZZZ
        <span class="a">
            AAA
        </span>
        <span class="ab">
            BBB
        </span>
        <span class="abc">
            CCC
            <preserveMe>
                eeee
            </preserveMe>
        </span>
        <span class="ab">
            bbb
            <preserveMe>
                eeee
            </preserveMe>
        </span>
        <span class="a">
            aaa
        </span>
    </p>
</body>

这是我最近的一次(我真的很陌生,所以即使走到这一步也花了我很长时间...)

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="*/span">
      <span class='{concat(../../@class,../@class,@class)}'>
           <xsl:value-of select='.'/>
       </span>
       <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

如果您自己 运行 可以看到我尝试失败的结果,以及与我真正想要的结果相差多少。理想情况下,我想要一个接受任意数量的嵌套级别并且还可以处理中断的嵌套(span、span、notSpan、span...)的解决方案。

编辑:我已经根据下面评论者的请求在嵌套结构中添加了标签。另外,我使用的是 XSLT v1.0,但我想如果需要我可以使用其他版本。

编辑 2:我意识到与我实际需要转换的内容相比,我的示例过度简化了。也就是说,我不能从其他标签中丢失 classes;只能合并 span。

希望以下样式表对您有所帮助:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- Identity transform template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="p">
        <xsl:copy>
            <xsl:apply-templates select="@* | text() | .//text()[parent::span]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()[parent::span]">
        <span>
            <xsl:attribute name="class">
                <xsl:call-template name="class-value"/>
            </xsl:attribute>
            <xsl:value-of select="."/>
            <xsl:apply-templates select="following-sibling::node()[1][not(self::text()) and not(self::span)]"/>
        </span>
    </xsl:template>

    <xsl:template name="class-value">
        <xsl:for-each select="ancestor::span/@class">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

给你..我已经通过带有两个参数的嵌套跨度模板递归地完成了它,第一个是当前跨度 class 连接 classes 和当前跨度节点。然后处理嵌套的跨度。

因此,在我们的案例中,我只是将根跨度的模板称为 p 标签下的 span

<xsl:template match="/">
  <hmtl>
    <body>
    <p>
    <xsl:for-each select='.//p/span'>
        <xsl:call-template name='nested-span'>
            <xsl:with-param name='cclass' select='./@class'></xsl:with-param>
            <xsl:with-param name='cspan' select='.'></xsl:with-param>
        </xsl:call-template>
    </xsl:for-each>
    </p>
    </body>
  </hmtl>
</xsl:template>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>


 <xsl:template name="nested-span">
        <xsl:param name='cclass'/>
        <xsl:param name='cspan' as='node()' />
        <span>
                <xsl:attribute name='class' select='$cclass'/>
                <xsl:value-of select='$cspan/text()[1]' />
                <xsl:if test="not(exists(./span))">
                    <xsl:if test='string-length($cspan/text()[2]) &gt; 0 '>
                         <xsl:value-of select='$cspan/text()[2]' />
                    </xsl:if>
                    <xsl:apply-templates select="./*[local-name() != 'span']"/>
                </xsl:if>
        </span>

        <xsl:for-each select='$cspan/span'>
            <xsl:call-template name='nested-span'>
                <xsl:with-param name='cclass' select='concat($cclass, ./@class)' ></xsl:with-param>
                <xsl:with-param name='cspan' select='.'></xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>


        <xsl:if test="exists(./span)">
            <span>     
                <xsl:attribute name='class' select='$cclass'/>
                    <xsl:if test='string-length($cspan/text()[2]) &gt; 0 '>
                        <xsl:value-of select='$cspan/text()[2]' />
                    </xsl:if>
                <xsl:apply-templates select="./*[local-name() != 'span']"/>
            </span>
        </xsl:if>    
</xsl:template>

这是输出

<hmtl>
   <body>
      <p>
            <span class="a">
             AAA
            </span>
            <span class="ab">
               BBB
            </span>
            <span class="abc">
              CCC
              <preserveMe>
                eeee
              </preserveMe>
            </span>
            <span class="ab">
               bbb
               <preserveMe>
                  eeee
                </preserveMe>
            </span>
            <span class="a">
              aaa
            </span>
       </p>
   </body>
</hmtl>

希望对您有所帮助

正如我在开场评论中提到的,这绝非微不足道。这是您可以考虑的另一种方法:

XSLT 1.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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()|.//span/text()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="span/text()">
    <span>
        <xsl:attribute name="class">
            <xsl:for-each select="ancestor::span">
                <xsl:value-of select="@class"/>
            </xsl:for-each>
        </xsl:attribute>
        <xsl:apply-templates select="preceding-sibling::*"/>
        <xsl:value-of select="." />
        <xsl:if test="not(following-sibling::text())">
            <xsl:apply-templates select="following-sibling::*"/>
        </xsl:if>
    </span>     
</xsl:template>

<xsl:template match="span"/>

</xsl:stylesheet>

这在很大程度上类似于 Lingamurthy CS 之前的建议 - 但您看到与以下测试输入的不同之处:

XML

<body>
    <h1 class="section">Title</h1>
    <p class="main">
        ZZZ
        <preserveMe>0</preserveMe>
        <span class="a">
            AAA
            <span class="b">
                BBB
                <span class="c">
                    CCC
                    <preserveMe>c</preserveMe>
                </span>
                bbb
                <preserveMe>b</preserveMe>
            </span>
            aaa
        </span>
        <preserveMe>1</preserveMe>
    </p>
</body>

正如您所说的 XSLT 2.0 可能 是一个可行的选择,我尝试了一种基于节点分组的方法:

<xsl:stylesheet version="2.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:template match="* | @* | text()">
        <xsl:copy>
            <xsl:apply-templates select="* | @* | text()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="span">
        <xsl:for-each-group select="* | text()" group-adjacent="name() = 'span'">
            <xsl:choose>
                <xsl:when test="current-group()/self::span">
                    <!-- a group of span elements: nothing to do yet -->
                    <xsl:apply-templates select="current-group()"/>
                </xsl:when>
                <xsl:otherwise>
                    <!-- a group of text nodes and no-span elements: create span -->
                    <span class="{string-join((ancestor::span/@class), '')}">
                        <xsl:apply-templates select="current-group()"/>
                    </span>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

要点:

  • span 元素的子元素(包括文本节点和其他元素)根据它们是否 span 进行分组
  • 产生与
  • 相同的输出