如何解决使用XPATH在两个处理指令之间选择所有文本节点
我有一些以下XML:
<content>
<p>
<?change-addition-start?>
test
<em>test</em>
<strong>test</strong>
<?change-addition-end?>
</p>
</content>
我试图在<ins>
PI之后添加一个<?change-addition-start?>
标签,并在</ins>
之前添加一个结束<?change-addition-end?>
标签,因此基本上只是尝试在它们之间包装任何内容<ins>
标签中的两个PI。有没有办法用XSLT / XPATH做到这一点?这些标记之间可以包含任何内容,因此无法针对我上面的测试用例进行特定设置。
解决方法
我建议使用Maring Honnen在第一条评论中建议的xal:for-each-group
。其他XPath方法允许您在两个处理指令之间选择内容,但是很难将其嵌入到xslt样式表中,该样式表也应该做其他事情,例如同时复制现有结构。这里是使用for-each-group的最小方法:
xquery version "1.0-ml";
let $xml :=
<content>
<p>
before
<?change-addition-start?>
test
<em>test</em>
<strong>test</strong>
<?change-addition-end?>
after
</p>
</content>
let $xsl :=
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[processing-instruction()]">
<xsl:variable name="parent" select="." />
<xsl:for-each-group select="node()" group-starting-with="processing-instruction()">
<xsl:choose>
<xsl:when test="self::processing-instruction('change-addition-start')">
<ins>
<xsl:apply-templates select="current-group()" mode="identity"/>
</ins>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" mode="identity"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
return xdmp:xslt-eval($xsl,$xml)
如果您想在<ins>
标记内获得结束PI,请使用Martin建议的第二个for-each-group和group-end-by。以上可能就足够了。
HTH!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。