[英]Select all siblings (including text) of comment in xpath
I have an XML document, a fragment line of which could look like this: 我有一个XML文档,其片段行可能看起来像这样:
<p>Some text <!--a comment --> some more text <b>some bold text</b> something else etc</p>
I would like to select the comment based on its text, but also all following "sibling" elements. 我想根据其文本选择注释,但也要选择所有以下“兄弟”元素。 In this example, I know I can get the comment with '//comment()[. 在此示例中,我知道可以使用'// comment()[获得注释。 = "a comment"]'. =“评论”]'。
How can I get the result: " some more text some bold text something else etc"? 我如何得到结果:“更多文本, 其他粗体文本等等”? (the remainder of the siblings inside the paragraph tag) (段落标记内的其余兄弟姐妹)
In case it makes any difference, I'm using python and etree to parse. 如果有什么不同,我正在使用python和etree进行解析。
EDIT: 编辑:
My test XML in full: 我完整的测试XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>A paragraph<!--A comment--><b>test</b>A line break</p>
</root>
My test XSLT: 我的测试XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:copy-of select='//comment()/following-sibling::node()'/>
</xsl:template>
</xsl:stylesheet>
The result: 结果:
<?xml version="1.0" encoding="UTF-8"?>
or, in Python, using lxml, just a "None" object. 或者,在Python中,使用lxml,只是一个“无”对象。
EDIT #2: 编辑#2:
My bad -- the accepted answer works well! 我的错-可接受的答案效果很好!
If you want to get all siblings including other comments: 如果您想获得所有兄弟姐妹,包括其他评论:
//comment()[.="a comment "]/following-sibling::node()
For example: 例如:
>>> xml.xpath('//comment()[.="a comment "]/following-sibling::node()')
[' some more text ', <Element b at 0x2923af0>, ' ', <!-- other comment -->, ' something else etc']
I added an additional comment but otherwise used your input data. 我添加了一条附加注释,但使用了您的输入数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.