python - 使用 xpath 通过属性 ID 和文本查找节点
问题描述
我了解如何通过属性 ID 或文本查找节点,但不确定两者的语法。我正在使用 XML 库,我无权访问,lxml
因为无法将它安装在我的工作计算机上。这是我正在使用的 XML 的摘录。
<product product-id="000100015BA00-00823">
<ean/>
<upc/>
<page-attributes/>
<custom-attributes>
<custom-attribute attribute-id="color">000100015BA00-00823</custom-attribute>
</custom-attributes>
<pinterest-enabled-flag>false</pinterest-enabled-flag>
<facebook-enabled-flag>false</facebook-enabled-flag>
</product>
<product product-id="000100103H103-07546">
<ean/>
<upc/>
<unit/>
<min-order-quantity>1</min-order-quantity>
<custom-attributes>
<custom-attribute attribute-id="color">000100103H103-07546</custom-attribute>
</custom-attributes>
<pinterest-enabled-flag>false</pinterest-enabled-flag>
</product>
这是我尝试过的:
for x in root.findall('dw:product/dw:custom-attributes/dw:custom-attribute[@attribute-id="color" and text()="000100103H103-07546"]', ns):
print x.text
但我收到此错误消息:SyntaxError: invalid predicate
我认为这是因为text()
不适用于 XML 包。有没有办法做到这一点lxml
?
解决方案
推荐阅读
- sqlite - Flutter:如何从 sqlite 同步数据?
- jquery - Laravel 会话和表列隐藏显示与 jquery 用户偏好
- python - 使用列名从 pandas 调用列时出错
- python - 如何使用 Python 代码以表格格式打印多个 txt 文件的输出?
- jquery - 如何在月份编号的文本中显示月份
- python - 通过正则表达式更新数据框下的列
- reactjs - 如何将参数传递给 createAsyncThunk?
- scala - Intellij IDEA 无法解析隐含符号(scala tagless final)
- javascript - 除非我真正查看网页,否则 Selenium 将无法工作(也许是 JavaScript 的反爬虫机制?)
- java - 安装詹金斯,无法在 Windows 10 中打开端口 8080