访问下一个同级的文本

2024-09-29 22:00:37 发布

您现在位置:Python中文网/ 问答频道 /正文

这是jenkins xml文件的一部分。在

我想用xpath提取project_name的默认值。在

在这种情况下,值是*****。在

<?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知识有限。我目前的做法是:

^{pr2}$

这里我得到AttributeError: 'Element' object has no attribute 'getparent'

我又考虑过这个问题,我认为在python中循环是错误的方法。这应该可以通过xpath选择。在


Tags: 文件nameprojectmodelcustomerdescriptionxmlproperties
2条回答

您可以尝试lxml.etree如下-我使用循环来选择具有相同位置的所有节点。在

所需xpath的示例是-我使用了relative xpath,因为它在长节点路径的情况下非常有用。在

.//hudson.model.StringParameterDefinition/name[contains(text(),'project_name')]/following-sibling::defaultValue

或者

.//hudson.model.StringParameterDefinition/name[contains(text(),'project_name')]/following::defaultValue[1]

^{pr2}$

输出-

['my_customer', '*****']
['*****']
['*****']

问题的答案就是XPath

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

它将选择作为唯一的结果

^{pr2}$

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

即使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]

输出:

^{pr2}$

相关问题 更多 >

    热门问题