Xml 使用XSLT2.0和Xpath在处理指令之间添加标记
我尝试在两条处理指令之间包装所有节点,但由于PI的性质和位置,这很棘手: 示例输入数据:Xml 使用XSLT2.0和Xpath在处理指令之间添加标记,xml,xslt,xpath,xslt-2.0,Xml,Xslt,Xpath,Xslt 2.0,我尝试在两条处理指令之间包装所有节点,但由于PI的性质和位置,这很棘手: 示例输入数据: <content> <p>Para 1 <?start?>changes <i>here</i></p> <x>Para 2 <b>changes</b> here <?end?> and continues here</x> <y>Para 3 her
<content>
<p>Para 1 <?start?>changes <i>here</i></p>
<x>Para 2 <b>changes</b> here <?end?> and continues here</x>
<y>Para 3 here</y>
<z>Para <?start?>4 here <i>with more</i> tags</z>
<a>Para 5 starts here</a>
<b>Para 6 starts here</b><?end?>
</content>
第1款在此作了修改
第2段在此更改,并在此处继续
第3段
第4段这里有更多的标签
第5段从这里开始
第6段从这里开始
期望输出:
<content>
<p>Para 1 <ins>changes <i>here</i></ins></p>
<x><ins>Para 2 <b>changes</b> here </ins> and continues here</x>
<y>Para 3 here</y>
<z>Para <ins>4 here <i>with more</i> tags</ins></z>
<a><ins>Para 5 starts here</ins></a>
<b><ins>Para 6 starts here</ins></b>
</content>
第1款在此作了修改
第2段在此更改,并在此处继续
第3段
第4段这里有更多的标签
第5段从这里开始
第6段从这里开始
对于和pi,我想将它们之间的所有节点包装在一个元素中
PI也可以是任何元素的一部分,这又增加了一个折痕
这里的棘手问题是
和
pi可以在一个元素中开始,在另一个元素中结束(请参见示例输入中的元素p和x),在这种情况下,我需要将每个元素包装在每个自己的
标记中(以创建有效的xml),然后在下一个元素上重新开始,直到它到达
pi。使用XSLT 2.0实现所需输出的任何方法?与
只需要对代码进行一些修改,如下所示
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<xsl:variable name="Cleanup">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates select="$Cleanup" mode="Fixes"/>
</xsl:template>
<xsl:template match="*[processing-instruction('start')][not(processing-instruction('end'))]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<xsl:processing-instruction name="end"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[processing-instruction('end')][not(processing-instruction('start'))][preceding::processing-instruction('start')]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:processing-instruction name="start"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(processing-instruction('start'))][preceding-sibling::*[processing-instruction('start')][not(processing-instruction('end'))]][preceding::processing-instruction()[1][self::processing-instruction('start')]]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:processing-instruction name="start"/>
<xsl:apply-templates/>
<xsl:processing-instruction name="end"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node() | @*" mode="#all">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('start')" mode="Fixes">
<xsl:variable name="piend"
select="following-sibling::processing-instruction('end')[1]/generate-id()"/>
<ins>
<xsl:copy-of
select="following-sibling::node()[following-sibling::processing-instruction('end')[generate-id() = $piend]]"
/>
</ins>
</xsl:template>
<xsl:template
match="node()[preceding::processing-instruction()[1][self::processing-instruction('start')]][following::processing-instruction()[1][self::processing-instruction('end')]]" mode="Fixes"> </xsl:template>
<xsl:template match="processing-instruction('end')" mode="Fixes">
</xsl:template>
</xsl:stylesheet>
请参见上的转换。所有PIs都是
?@michael.hor257k的子元素吗?不,它们可以是任何元素的子元素,让我将其添加到我的postAny连接或@michael.hor257k中进行编辑。不,这很相似,但我的输入比这个问题复杂得多