You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按指定location值查找support_calender中含remark、supportType的条目?

解决方案

1. 使用XPath直接查询

针对你的XML结构,可使用以下XPath表达式定位符合条件的条目:

/support_calender/date/*[location='HC21' and exists(remark) and exists(supportType)]
  • 表达式说明:
    • /support_calender/date/*:遍历support_calender下所有date节点的子条目
    • location='HC21':匹配地点值为指定值的条目
    • exists(remark) and exists(supportType):筛选同时包含remark和supportType子节点的条目

如果需要匹配动态地点值,只需把'HC21'替换为目标地点字符串即可。

2. Python代码实现(内置xml.etree库)

如果需要用代码批量处理,可借助Python内置的xml.etree.ElementTree库实现:

import xml.etree.ElementTree as ET

# 加载XML文件(或解析XML字符串)
tree = ET.parse("your_xml_file.xml")
root = tree.getroot()

target_location = "HC21"
# 查找符合条件的条目
matched_entries = root.findall(f"./date/*[location='{target_location}' and remark and supportType]")

# 遍历输出结果
for entry in matched_entries:
    print("找到符合条件的条目:")
    print(f"地点: {entry.find('location').text}")
    print(f"备注: {entry.find('remark').text}")
    print(f"支持类型: {entry.find('supportType').text}")
    print("---")

3. Python代码实现(lxml库,支持更完整的XPath语法)

如果需要更强大的XPath支持,推荐使用lxml库:

from lxml import etree

tree = etree.parse("your_xml_file.xml")
target_location = "HC21"
# 使用完整XPath表达式查询
matched_entries = tree.xpath(f"/support_calender/date/*[location='{target_location}' and exists(remark) and exists(supportType)]")

for entry in matched_entries:
    # 将条目转为字符串查看完整内容
    print(etree.tostring(entry, encoding='utf-8').decode('utf-8'))

内容的提问来源于stack exchange,提问作者Tony Tong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 18:50:58