使用 XSLT 1.0 在每两个处理指令之间对 XML 节点进行分组 [英] Grouping XML nodes between each two processing instructions using XSLT 1.0

查看:21
本文介绍了使用 XSLT 1.0 在每两个处理指令之间对 XML 节点进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 XSLT 1.0,我需要在处理指令 之间对每个节点进行分组.这对之外的节点在输出端应该保持不变.

Using XSLT 1.0, I need to group each node between processing instructions <?start?> and <?end?>. The nodes outside of this pair should be unchanged at the output.

首先,我需要找到一种方法,如何仅选择每个开始 - 结束对之间的节点.假设我们有一个示例输入 XML:

First, I need to find a way how to select only the nodes that are between each start - end pair. Suppose we have an example input XML:

<root>
  abc
  <?start?>
    def<Highlighted bold="yes">
    <Highlighted italic="yes">ghi</Highlighted>
    </Highlighted>jkl
    <?pi?>
    <table>
      <Caption>stu</Caption>
    </table>vw
  <?end?>
  xy
  <?start?> 
  abc <Caption>def</Caption> ghi
  <?end?>
  jkl
</root>

此外,我还需要在输出的开始 - 结束"交叉点之外有节点.这意味着在输出中:a) 开始 - 结束 PI 交点处的节点将在组元素中 b) 交点外的任何节点将被打印不变.注意输入文档也可能没有开始-结束处理指令对.

Furthermore, I need to have nodes OUTSIDE the "start - end" intersections at the output as well. This means that at the output: a) nodes at the intersection of start - end PI will be in the group element b) any node outside the intersection will be printed unchanged. Note that the input document may also have no start - end processing instruction pair.

例如,从给定的输入,输出应该如下:

For example, from the given input, the output should be as follows:

<root>
  abc
  <group>
    def<Highlighted bold="yes">
    <Highlighted italic="yes">ghi</Highlighted>
    </Highlighted>jkl
    <?pi?>
    <table>
      <Caption>stu</Caption>
    </table>vw
   </group>
   xy
   <group> 
     abc <Caption>def</Caption> ghi
   </group>
   jkl
 <root>

这个问题的一部分已经在寻找每两个处理指令之间的所有 XML 节点.但是我很难在不复制任何节点的情况下打印出组元素之外的节点.

A part of this question has already been answered in Finding all XML nodes between each two processing instructions. But I struggle with printing out nodes outside of the group element without duplicating any node.

推荐答案

代码:

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

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

    <xsl:template match="processing-instruction('start')">
        <group>
            <xsl:apply-templates select="following-sibling::node()[1]"/>
        </group>
        <xsl:apply-templates select="following-sibling::processing-instruction('end')[1]/following-sibling::node()[1]"/>
    </xsl:template>

    <xsl:template match="processing-instruction('end')"/>

</xsl:stylesheet>

此 XSLT 使用递归方法处理第一个子节点和第一个后续兄弟节点().

This XSLT uses a recursive approach of processing first child node and the first following-sibling node().

第一个模板处理除start和end pis之外的所有节点.第二个模板将 group 元素添加到起始 pi 和结束 pi 之间的元素.第三个模板有助于去除结尾的圆周率.

The first template processes all nodes except start and end pis. The second template adds the group element to the elements between start pi and end pi. Third template helps remove the end pi.

这篇关于使用 XSLT 1.0 在每两个处理指令之间对 XML 节点进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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