处理指令变换 [英] Processing instructions transform

查看:19
本文介绍了处理指令变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将源 xml 中的处理指令转换为输出中的某个标记

I want to transform processing instructions in a source xml to some tag in an output

输入

<?xml version="1.0" encoding="utf-8"?>
<root>
    <?PI_start?> SOME TEXT <?PI_end?>
</root>

我想要这样的输出 xml

<root>
    <tag> SOME TEXT </tag>
</root>

我可以吗?如果是,我必须使用什么 xsl 进行转换?

Can I do it? If yes what xsl must I use for transform?

我只找到了一种将 PI 转换为开始和结束标记的方法.PI 可以包含一些内容.

I found only a way to transform PIs to the opening and closing tags. PI can contain some content.

输入 XML

<root>
    <?PI SOME TEXT?>
</root>

XSL

<xsl:template match="processing-instruction('PI')">
    <tag><xsl:value-of select="."/></tag>
</xsl:template>

输出

<tag>SOME TEXT</tag>

但这有点不是我的情况

推荐答案

这种转变:

<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="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="processing-instruction('PI_start')">
  <tag>
   <xsl:apply-templates mode="copy" select=
       "following-sibling::node()[1][self::text()]"/>
  </tag>
 </xsl:template>

 <xsl:template match=
 "processing-instruction('PI_end')
 |
  text()[preceding-sibling::node()[1]
              [self::processing-instruction('PI_start')]]
 "/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <?PI_start?> SOME TEXT <?PI_end?>
</root>

产生想要的、正确的结果:

<root>
   <tag> SOME TEXT </tag>
</root>

请注意:

  1. 身份规则用于按原样"复制所有节点.

  1. The identity rule is used to copy all nodes "as-is".

我们有额外的模板,仅用于应以某种方式更改的节点.

匹配第一个 PI 的模板几乎完成所有工作". 它创建一个 tag 元素并将模板应用到以下兄弟节点,如果它是一个PI.

The template matching the first PI "does almost all the work". It creates a tag element and applies templates to the following-sibling node if it is a PI.

我们以复制"模式为第一个 PI 的文本节点的直接兄弟应用模板.

没有在任何地方声明复制"模式,这会导致选择处理文本节点的默认模板——它的动作只是复制文本节点.这是一个技巧,使我们无需在复制"模式下定义模板.

The mode "copy" isn't declared anywhere and this causes the default template for processing text nodes to be selected -- its action is to just copy the text node. This is a trick that saves us from the need to define a template in the "copy" mode.

我们有一个实际删除不需要的节点的空模板:第二个 PI 以及第一个 PI 的直接兄弟文本节点的第二个副本.

We have an empty template that actually deletes the unwanted nodes: the second PI and what would be a second copy of the first PI's immediate-sibling text node.

更新:OP 表示他也对两个 PI 之间可能存在不同节点(不仅是文本节点)的情况感兴趣.

Update: The OP has indicated that he is also interested in the case where in-between the two PIs there might be different nodes (not only text nodes).

这是一项复杂得多的任务,以下是一种解决方案:

<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:key name="kSurrounded" match="node()"
  use="concat(
        generate-id(preceding-sibling::processing-instruction('PI_start')[1]),
        '+++',
        generate-id(following-sibling::processing-instruction('PI_end')[1])
             )"/>

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

 <xsl:template match="processing-instruction('PI_start')">
  <tag>
   <xsl:apply-templates mode="copy" select=
       "key('kSurrounded',
             concat(generate-id(),
                   '+++',
                   generate-id(following-sibling::processing-instruction('PI_end')[1])
                   )
             )"/>
  </tag>
 </xsl:template>

 <xsl:template match=
 "processing-instruction('PI_end')
 |
  node()[(preceding-sibling::processing-instruction('PI_start')
         |
          preceding-sibling::processing-instruction('PI_end')
          )
           [last()][self::processing-instruction('PI_start')]
        and
         (following-sibling::processing-instruction('PI_start')
        |
          following-sibling::processing-instruction('PI_end')
          )
           [1][self::processing-instruction('PI_end')]
        ]
 "/>

 <xsl:template match="node()" mode="copy">
  <xsl:call-template name="identity"/>
 </xsl:template>
</xsl:stylesheet>

当上述转换应用于以下 XML 文档时:

<root>
    <?PI_start?> <strong>Some</strong> TEXT <?PI_end?> XA <?PI_end?>
</root>

产生想要的、正确的输出:

<root>
    <tag>
        <strong>Some</strong> TEXT 
    </tag> XA 
</root>

这篇关于处理指令变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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