通过使用多变量条件 lxml python 搜索无效谓词

invalid predicate by searching with multiple variable condition lxml python

我尝试使用 lxml 从 XML 文件中解析特定阶段的特定街道:

XML 文件的一部分:

<jndi_values environment="Ontwikkel" street="str1">
  <jndi_value key="ecm-eds-configVersie48str1a" value="eds/config/valstr1a">ONT_Value_3str1a</jndi_value>
  <jndi_value key="str1a" value="eds/config/valstr1">ONT_Value_WWW4a</jndi_value>
</jndi_values>
<jndi_values environment="Ontwikkel" street="str2">
  <jndi_value key="ecm-eds-configVersie48str2a" value="eds/config/str2a">ONT_Value_str2a</jndi_value>
  <jndi_value key="4WWW8str2a" value="eds/config/cW8str2">ONT_Value_4str2a</jndi_value>
</jndi_values>

我想查找例如 environment="Ontwikkel" 和 street="str2",仅这些值。

我试过了:

only_jndi_values = package_config.findall("jndi_values[@environment='" + current_environment + "' and street='" + current_street + "']/jndi_value")

only_jndi_values = package_config.findall("jndi_values[@environment='" + current_environment + "' and @street='" + current_street + "']/jndi_value")

但它抛出这个无效谓词错误:

File "src/lxml/etree.pyx", line 1553, in lxml.etree._Element.findall
File "src/lxml/_elementpath.py", line 322, in lxml._elementpath.findall
File "src/lxml/_elementpath.py", line 300, in lxml._elementpath.iterfind
File "src/lxml/_elementpath.py", line 283, in lxml._elementpath._build_path_iterator
File "src/lxml/_elementpath.py", line 229, in lxml._elementpath.prepare_predicate
SyntaxError: invalid predicate

如何使用 lxml findall() 评估 2 个属性的值?

.//jndi_values[@environment="Develop"][@street="str2"]


from lxml import etree

data = '''\
<foo>
<bar>
<jndi_values environment="Ontwikkel" street="str1">
  <jndi_value key="ecm-eds-configVersie48str1a" value="eds/config/valstr1a">ONT_Value_3str1a</jndi_value>
  <jndi_value key="str1a" value="eds/config/valstr1">ONT_Value_WWW4a</jndi_value>
</jndi_values>
<jndi_values environment="Ontwikkel" street="str2">
  <jndi_value key="ecm-eds-configVersie48str2a" value="eds/config/str2a">ONT_Value_str2a</jndi_value>
  <jndi_value key="4WWW8str2a" value="eds/config/cW8str2">ONT_Value_4str2a</jndi_value>
</jndi_values>
</bar>
</foo>
'''

environment = "Ontwikkel"
street = "str2"

tree = etree.fromstring(data)
xpath = f'.//jndi_values[@environment="{environment}"][@street="{street}"]'

print(xpath)

for e in tree.findall(xpath):
    for s in e.itertext():
        print(repr(s))