String Xpath选择节点文本但排除子节点文本
我有以下XML:String Xpath选择节点文本但排除子节点文本,string,xpath,tags,String,Xpath,Tags,我有以下XML: <topic class="Top"> <title> Interesting Article </title> <subtitle> Science & Industry <insertedText action="start"/> Inside & Out <insertedText
<topic class="Top">
<title>
Interesting Article
</title>
<subtitle>
Science & Industry
<insertedText action="start"/>
Inside & Out
<insertedText action="end"/>
A Profile
</subtitle>
</topic>
回答
根据@lambo477提供的答案,完整答案如下:
./topic[@class='top']/*[local-name()='subtitle']/text()[1]|/topic[@class='top']/*[local-name()='subtitle']/*[local-name()='insertedText'][@action='end']/following-sibling::node()[1]
您可以尝试以下XPath:
//subtitle/text()[1]|//insertedText[@action="end"]/following-sibling::node()[1]
您可以尝试以下XPath:
//subtitle/text()[1]|//insertedText[@action="end"]/following-sibling::node()[1]
谢谢@lambo477,不过我已经编辑了我的OP来澄清,节点的数量也是可变的。我认为我的解决方案应该以任何一种方式工作?它将在孩子们点头之前和之后选择文本Sah yes@lambo477,我现在看到了。尽管在某些情况下,标签的数量可能超过一个或没有!为了清晰起见,我再次扩展了我的OP,再次感谢。希望的输出是什么?例如,2?我的解决方案也适用于有0、1或>1个子元素的情况,因此我看不到问题!谢谢@lambo477,不过我已经编辑了我的OP来澄清,节点的数量也是可变的。我认为我的解决方案应该以任何一种方式工作?它将在孩子们点头之前和之后选择文本Sah yes@lambo477,我现在看到了。尽管在某些情况下,标签的数量可能超过一个或没有!为了清晰起见,我再次扩展了我的OP,再次感谢。希望的输出是什么?例如,2?我的解决方案也适用于有0、1或>1个子元素的情况,因此我看不到问题!