使用 XSLT 合并相邻的兄弟节点 [英] Merge adjacent sibling nodes with XSLT

查看:29
本文介绍了使用 XSLT 合并相邻的兄弟节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让我非常头疼.请帮我.输入是:

<p class="section">第 1 节 </p><p class="代码">一些代码</p><p class="代码">以下代码</p><p class="代码">以下代码</p><p class="section">第2节</p><p class="代码">其他代码

<p class="代码">以下代码</p><p class="代码">以下代码</p><p class="section">第 3 节 </p><p class="代码">还有其他代码</p><p class="代码">跟随</p><p class="代码">跟随</p>

我想要的输出:

<p class="section">第 1 节 </p><预>一些代码跟在代码之后的代码</pre><p class="section">第2节</p><预>其他代码跟在代码之后的代码</pre><p class="section">第 3 节 </p><预>在</pre>之后还有其他代码

问题是将所有相邻的 <p class="code"> 标签折叠为

 标签.不要找到使用 XSLT 执行此操作的方法.您认为存在解决方案吗?

解决方案

使用 XSLT 2.0,您可以使用 for-each-group group-adjacent 如下:

<xsl:output indent="yes"/><xsl:template match="@* | node()"><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy></xsl:模板><xsl:template match="body"><xsl:copy><xsl:for-each-group select="*" group-adjacent="boolean(self::p[@class = 'code'])"><xsl:when test="current-grouping-key()"><预><xsl:apply-templates select="current-group()/node()"/>

</xsl:when><xsl:否则><xsl:apply-templates select="current-group()"/></xsl:否则></xsl:选择></xsl:for-each-group></xsl:copy></xsl:模板></xsl:stylesheet>

您可以将 XSLT 2.0 与 Saxon 9AltovaXML 工具.

I have a question that caused me a terrible headache. Please help me. The input is:

<body>
 <p class="section"> section 1 </p>
 <p class="code"> some code </p>
 <p class="code"> following code </p>
 <p class="code"> following code </p>
 <p class="section"> section 2 </p>
 <p class="code"> other code </p>
 <p class="code"> following code </p>
 <p class="code"> following code </p>
 <p class="section"> section 3 </p>
 <p class="code"> still other code </p>
 <p class="code"> following </p>
 <p class="code"> following </p>
</body>

The output I'd like:

<body>
 <p class="section"> section 1 </p>
 <pre> some code following code following code </pre>
 <p class="section"> section 2 </p>
 <pre> other code following code following code </pre>
 <p class="section"> section 3 </p>
 <pre> still other code following following </pre>
</body>

The problem is to collapse to a <pre> tag all adjacent <p class="code"> tags. Don't find a way to do this using XSLT. Do you think a solution exists?

解决方案

With XSLT 2.0 you can use for-each-group group-adjacent as follows:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:output indent="yes"/>

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

  <xsl:template match="body">
    <xsl:copy>
      <xsl:for-each-group select="*" group-adjacent="boolean(self::p[@class = 'code'])">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <pre>
              <xsl:apply-templates select="current-group()/node()"/>
            </pre>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

You can use XSLT 2.0 with Saxon 9 or with AltovaXML tools.

这篇关于使用 XSLT 合并相邻的兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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