python - 访问下一个同级的文本

标签 python xml xpath jenkins elementtree

这是 jenkins xml 文件的一部分。

我想使用 xpath 提取 project_name 的 defaultValue。

在本例中,该值为 *****

<?xml version='1.0' encoding='UTF-8'?>
<project>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>customer_name</name>
                    <description></description>
                    <defaultValue>my_customer</defaultValue>
                </hudson.model.StringParameterDefinition>
                <hudson.model.StringParameterDefinition>
                    <name>project_name</name>
                    <description></description>
                    <defaultValue>*****</defaultValue>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
 </project>

我使用python的etree,但是据我所知这并不重要,因为这是一个xpath问题。

我目前的 xpath 知识有限。我目前的做法:

for name_tag in config.findall('.//name'):
    if name_tag.text=='project_host':
        default=name_tag.getparent().findall('defaultValue')[0].text

这里我得到AttributeError:'Element'对象没有属性'getparent'

我又想了一下,我认为在Python中循环是错误的方法。这应该可以通过 xpath 选择。

最佳答案

您的问题的 XPath 答案是

/project/properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/hudson.model.StringParameterDefinition[name = 'project_name']/defaultValue/text()

这将选择作为唯一的结果

*****

鉴于您的实际文档没有命名空间。您不需要访问父元素或同级轴。

即使是 etree 也应该支持这种 XPath 表达式,但也可能不支持 - 请参阅 comment by har07 .


I thought about this again, and I think that looping in python is the wrong approach. This should be selectable via xpath.

是的,我同意。如果要从文档中选择单个值,请使用 XPath 表达式选择它并将其直接存储为 Python 字符串,而无需循环遍历元素。


lxml 的完整示例

from lxml import etree
from StringIO import StringIO

document_string = """<project>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>customer_name</name>
                    <description></description>
                    <defaultValue>my_customer</defaultValue>
                </hudson.model.StringParameterDefinition>
                <hudson.model.StringParameterDefinition>
                    <name>project_name</name>
                    <description></description>
                    <defaultValue>*****</defaultValue>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
 </project>"""

tree = etree.parse(StringIO(document_string))

result_list = tree.xpath("/project/properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions/hudson.model.StringParameterDefinition[name = 'project_name']/defaultValue/text()")

print result_list[0]

输出:

*****

关于python - 访问下一个同级的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35174547/

相关文章:

python - Qt5 Matplotlib 设计器插件

python - Pandas 中的自加入产生不需要的重复项

xpath - lxml xpath 函数返回的元素是否按顺序返回?

python - 将具有嵌套列表的列转换为以第一个元素作为列名的列

python - 保存并从磁盘重新加载后获取 Keras 模型的(输入)层

c# - 反序列化大型 XML 文档中的单个元素 : xmlSerializer. Deserialize(xmlReader.ReadSubtree()) 由于命名空间问题而失败

java - 无法使用 JDOM 和 Xpath 提取 xml 值元素

android - 您可以使用 XML 将 View 连接到事件处理程序吗?

python - 如何在 Glassdoor 上使用 Selenium 和 Python 访问 iframe

google-chrome - 为什么谷歌浏览器不打印我的 XPath 查询的 text()?