XPath技术问题:如何排除子节点中的指定文本内容
解决TEI文档中提取节点文本并排除指定内容的XPath方案
你的问题核心是不要排除整个节点,而是精准过滤内部不需要的文本内容。以下是满足需求的XPath表达式及说明:
最终XPath表达式
TEI//div/u//text()[ not(parent::desc or parent::vocal) and not(preceding-sibling::anchor and following-sibling::anchor) and not(ancestor::*[preceding-sibling::anchor and following-sibling::anchor]) ]
表达式拆解
TEI//div/u//text():定位到下节点内的所有文本节点(包括嵌套节点里的文本)not(parent::desc or parent::vocal):直接排除、 节点自身包含的文本 not(preceding-sibling::anchor and following-sibling::anchor):排除直接夹在两个兄弟节点之间的文本 not(ancestor::*[preceding-sibling::anchor and following-sibling::anchor]):排除那些嵌套在“... ”区间内的其他节点(比如示例里的 )中的文本
补充说明
如果需要将筛选后的文本合并为一段并去除多余空白,可以使用normalize-space()包裹整个表达式:
normalize-space(string-join(TEI//div/u//text()[ not(parent::desc or parent::vocal) and not(preceding-sibling::anchor and following-sibling::anchor) and not(ancestor::*[preceding-sibling::anchor and following-sibling::anchor]) ], ''))
你之前用的TEI//u[not(desc)]逻辑错误,它是在筛选“不包含
内容的提问来源于stack exchange,提问作者Rosmerade
相关产品推荐
相关产品推荐

