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

在XSLT 2.0中将不同层级的节点合并为单个列表

Extracting and Merging All <person> Nodes into a Single List

Got it, let's figure out how to pull every <person> node from your XML and combine them into one clean list. Below are two practical approaches depending on the tools you prefer to use:

1. Using Python's Built-in XML Library

If you're working with code, Python's xml.etree.ElementTree makes this straightforward. First, let's assume your full XML looks something like this (filling in the truncated parts from your example):

<group groupName="The Little Rascals">
  <peopleInGroup>
    <people>
      <person>
        <name value="John Doe"/>
        <birthdate value="01/01/1953"/>
      </person>
      <person>
        <name value="John Doe 2"/>
        <birthdate value="01/01/1953"/>
      </person>
    </people>
    <childrenInGroup>
      <person>
        <name value="Jane Doe"/>
        <birthdate value="01/01/1980"/>
      </person>
    </childrenInGroup>
  </peopleInGroup>
</group>

Here's the code to extract and merge all <person> nodes:

import xml.etree.ElementTree as ET

# Replace this with your actual XML content or file path
xml_content = """
<group groupName="The Little Rascals">
  <peopleInGroup>
    <people>
      <person>
        <name value="John Doe"/>
        <birthdate value="01/01/1953"/>
      </person>
      <person>
        <name value="John Doe 2"/>
        <birthdate value="01/01/1953"/>
      </person>
    </people>
    <childrenInGroup>
      <person>
        <name value="Jane Doe"/>
        <birthdate value="01/01/1980"/>
      </person>
    </childrenInGroup>
  </peopleInGroup>
</group>
"""

# Parse the XML
root = ET.fromstring(xml_content)

# Use XPath to find ALL <person> nodes (regardless of nesting level)
all_person_nodes = root.findall(".//person")

# Convert nodes into a structured list of dictionaries
merged_person_list = []
for person in all_person_nodes:
    person_data = {
        "name": person.find("name").get("value"),
        "birthdate": person.find("birthdate").get("value")
    }
    merged_person_list.append(person_data)

# Print the result
print(merged_person_list)

Output:

[{'name': 'John Doe', 'birthdate': '01/01/1953'}, {'name': 'John Doe 2', 'birthdate': '01/01/1953'}, {'name': 'Jane Doe', 'birthdate': '01/01/1980'}]

2. Using XPath Directly (For Tools Like XQuery, Dev Tools, or XML Editors)

If you just need to query the XML without writing code, use the XPath expression:

//person

This expression recursively searches through all levels of the XML to find every <person> node, no matter if it's under <people> or <childrenInGroup>. Most XML tools (like Oxygen XML, browser dev tools, or Postman) support this syntax to extract matching nodes.

Key Note:

The // in XPath is critical here—it ensures you don't miss any <person> nodes nested deep in the XML structure. If you used a more specific path like /group/peopleInGroup/people/person, you'd only get the top-level ones and miss the ones under <childrenInGroup>.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:21:21