简体   繁体   English

在xpath中选择评论的所有同级(包括文本)

[英]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.

相关问题 Xpath仅选择具有匹配属性的直接同级 - Xpath to select only direct siblings with matching attributes 得到所有 <div> 直到下一个兄弟姐妹 <div> 带有带有lxml.thml和XPath的特定文本 - Get all <div> siblings until next <div> with a specific text with lxml.thml and XPath XPath用于选择所有兄弟姐妹直到div - XPath for selecting all siblings until a div lxml XPath-过滤所有文本,包括尾部 - lxml XPath- filter all text including tails 如何使用XPath从内部获取所有文本 <p> 元素,包括任何href链接及其链接文本 - How to use XPath to get all text from within <p> element including any href links and their link text 通过使用BeautifulSoup选择所有div兄弟姐妹 - Select all div siblings by using BeautifulSoup 使用以下兄弟姐妹查找 xpath 并在 Python Selenium 中包含文本 - Find xpath with following-siblings and contains text in Python Selenium 如何在xpath中使用contains(text(),)时获取兄弟姐妹 - How to get siblings when using contains(text(), ) in xpath 如何选择所有子文本但使用Scapy的XPath排除标记? - How to select all children text but excluding a tag with Scapy's XPath? lxml xpath 表达式,用于选择给定子节点下的所有文本,包括他的子节点 - lxml xpath expression for selecting all text under a given child node including his children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM