在任何类似的问题中,可以使用Kayessian公式为nodeset交集。
如果我们有两个节点集$ns1
和$ns2
,然后同时属于这两个节点集所有节点都通过这个XPath表达式选择:
$ns1[count(.|$ns2) = count($ns2)]
你的情况,你有刚刚替补$ns1
与:
//w:r[w:fldChar/@w:fldCharType='begin'][1]/following-sibling::*
..
和替代$ns2
与:
//w:r[w:fldChar/@w:fldCharType='end'][1]/preceding-sibling::*
产生的XPath表达式可能看起来太复杂,但你获得的是非常容易地解决此类问题的能力,几乎机械:
/*/w:r
[w:fldChar/@w:fldCharType='begin']/following-sibling::*
[count(. | /*/w:r[w:fldChar/@w:fldCharType='end']
/preceding-sibling::*
)
=
count(/*/w:r[w:fldChar/@w:fldCharType='end']
/preceding-sibling::*)
]
基于XSLT的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="some:namespace">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/w:r
[w:fldChar/@w:fldCharType='begin']/following-sibling::*
[count(. | /*/w:r[w:fldChar/@w:fldCharType='end']
/preceding-sibling::*
)
=
count(/*/w:r[w:fldChar/@w:fldCharType='end']
/preceding-sibling::*)
]
"/>
</xsl:template>
</xsl:stylesheet>
当该转化此XML文档上施加:
<t xmlns:w="some:namespace">
<w:r>
<w:fldChar w:fldCharType="before-begin"/>
</w:r>
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText> DOCPROPERTY EvidenceBase \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:t>EvidenceBase</w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
<w:r>
<w:fldChar w:fldCharType="after-end"/>
</w:r>
</t>
完全所需元件被选择和复制到输出:
<w:r xmlns:w="some:namespace">
<w:instrText> DOCPROPERTY EvidenceBase \* MERGEFORMAT </w:instrText>
</w:r>
<w:r xmlns:w="some:namespace">
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r xmlns:w="some:namespace">
<w:t>EvidenceBase</w:t>
</w:r>
这是伟大的,完美的作品! – ScottD