我想将源xml中的处理指令转换为输出中的某个标记
输入
<?xml version="1.0" encoding="utf-8"?>
<root>
<?PI_start?> SOME TEXT <?PI_end?>
</root>
Run Code Online (Sandbox Code Playgroud)
我希望得到像这样的输出xml
<root>
<tag> SOME TEXT </tag>
</root>
Run Code Online (Sandbox Code Playgroud)
我可以做吗?如果是,我必须使用xsl进行转换?
我发现只有一种方法可以将PI转换为开始和结束标记.PI可以包含一些内容.
输入XML
<root>
<?PI SOME TEXT?>
</root>
Run Code Online (Sandbox Code Playgroud)
XSL
<xsl:template match="processing-instruction('PI')">
<tag><xsl:value-of select="."/></tag>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
产量
<tag>SOME TEXT</tag>
Run Code Online (Sandbox Code Playgroud)
但这不是我的情况
Dim*_*hev 10
这种转变:
<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>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的XML文档时:
<?xml version="1.0" encoding="utf-8"?>
<root>
<?PI_start?> SOME TEXT <?PI_end?>
</root>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<root>
<tag> SOME TEXT </tag>
</root>
Run Code Online (Sandbox Code Playgroud)
请注意:
该标识规则是用来复制所有节点"原样".
我们只为应该以某种方式更改的节点提供其他模板.
匹配第一个PI的模板"几乎完成所有工作".它创建一个tag元素,并将模板应用于以下兄弟节点(如果它是PI).
我们以模式"复制"为第一个PI的文本节点立即同级应用模板.
模式"copy"未在任何地方声明,这会导致选择处理文本节点的默认模板 - 其操作是仅复制文本节点.这是一个技巧,使我们无需在"复制"模式下定义模板.
我们有一个空模板,实际上删除了不需要的节点:第二个PI和第一个PI的直接同级文本节点的第二个副本.
更新:OP表示他也对两个PI之间可能存在不同节点(不仅是文本节点)的情况感兴趣.
这是一个更复杂的任务,这是一个解决方案:
<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>
Run Code Online (Sandbox Code Playgroud)
当上述转换应用于以下XML文档时:
<root>
<?PI_start?> <strong>Some</strong> TEXT <?PI_end?> XA <?PI_end?>
</root>
Run Code Online (Sandbox Code Playgroud)
生成所需的正确输出:
<root>
<tag>
<strong>Some</strong> TEXT
</tag> XA
</root>
Run Code Online (Sandbox Code Playgroud)