Xml 使用XSLT在两条处理指令之间包装所有节点
输入XML:Xml 使用XSLT在两条处理指令之间包装所有节点,xml,xslt,Xml,Xslt,输入XML: <content> <p> <?start?> test <em> test man </em> <strong>test man</strong> <?end?> do not grab this text <strong>or this text</strong> </p> </content> 试验 测试人员 测试人员 不要抓
<content>
<p>
<?start?>
test
<em>
test man
</em>
<strong>test man</strong>
<?end?>
do not grab this text
<strong>or this text</strong>
</p>
</content>
试验
测试人员
测试人员
不要抓取此文本
或此文本
所需的输出XML:
<content>
<p>
<ins>
test
<em>
test man
</em>
<strong>test man</strong>
</ins>
do not grab this text
<strong>or this text</strong>
</p>
</content>
试验
测试人员
测试人员
不要抓取此文本
或此文本
我试图将所有文本节点和节点包装在标记中的两条处理指令之间。有没有办法用XSLT2.0实现这一点
我的当前模板不起作用:
<xsl:variable name="pi-end" select="processing-instruction('end')" />
<xsl:template match="processing-instruction('start')">
<ins>
<xsl:apply-templates mode="copy" select="following-sibling::node()[. << $pi-end]"/>
</ins>
</xsl:template>
有没有一种方法可以通过设计一个模板来实现这一点,该模板匹配PI并获取以下所有节点,直到PI结束?我需要灵活性,因为这些PI可以位于数据中的任何位置和任何标记的内部。使用此选项
<?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="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('start')">
<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')]]"> </xsl:template>
<xsl:template match="processing-instruction('end')">
</xsl:template>
</xsl:stylesheet>
请参见上的转换,这与您的另一个问题有什么不同:如果您的pi end变量是全局变量,正如本文所示,那么它不会选择pi,因为它不是顶级变量。需要
/内容/处理说明(…)