![](/img/trans.png)
[英]XML: How to grab all text nodes and nodes between 2 processing instructions with XPath
[英]Selecting all text nodes in between two processing instructions with XPATH
我有一些以下 XML:
<content>
<p>
<?change-addition-start?>
test
<em>test</em>
<strong>test</strong>
<?change-addition-end?>
</p>
</content>
我試圖在<?change-addition-start?>
PI 之后添加一個<ins>
標簽,並在<?change-addition-end?>
之前添加一個結束</ins>
標簽,所以基本上只是試圖包裝任何東西<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-ending-by。 不過,以上可能就足夠了。
哼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.