如何从指定元素开始选择XML层级中同层级的所有子元素?
Got it, let's figure out how to select all sibling elements at the same XML level starting from a specific element. First, let's complete your partial XML example to make it easier to work with:
<teststep id="1"> <description>Teststep 1</description> <call-action id="2"> <parameters> <parameter key="starttime" /> <parameter key="duration" /> </parameters> <results> <store-result key="" destination=""/> </results> </call-action> <call-action id="3"> <parameters> <parameter key="endpoint" /> </parameters> <results> <store-result key="response" destination="output"/> </results> </call-action> </teststep>
The Go-To Tool: XPath
XPath is the standard way to query XML nodes, and it's perfect for this scenario. Here are the most useful expressions based on what you need:
1. Select All Siblings (Including the Starting Element)
If you want every child element of the starting element's parent (including the element you started with), use this expression:
//call-action[@id="2"]/../*
- Breakdown:
//call-action[@id="2"]finds your starting element.../jumps up to its parent (the<teststep>node), and*selects all child elements of that parent. - Result: This returns
<description>,<call-action id="2">, and<call-action id="3">.
2. Select All Siblings (Excluding the Starting Element)
If you want all siblings except the starting element itself, combine two sibling axes:
//call-action[@id="2"]/following-sibling::* | //call-action[@id="2"]/preceding-sibling::*
- Breakdown:
following-sibling::*grabs elements that come after your starting element,preceding-sibling::*grabs elements that come before, and|merges the two result sets. - Result: This returns
<description>and<call-action id="3">.
3. Select Only Same-Type Siblings
If you only want siblings that match the starting element's tag (e.g., only other <call-action> elements), target the tag directly:
//call-action[@id="2"]/../call-action
- Breakdown: Instead of using
*to select all children, specify the tag name (call-action) to filter results. - Result: This returns both
<call-action id="2">and<call-action id="3">.
Alternative: DOM Manipulation (Example with Python)
If you're working with code instead of pure XPath queries, here's how you'd do this using Python's built-in xml.etree.ElementTree:
import xml.etree.ElementTree as ET # Parse your XML file (or load from a string) tree = ET.parse("your_test_steps.xml") root = tree.getroot() # Locate your starting element start_element = root.find('.//call-action[@id="2"]') # Get the parent node of the starting element parent_node = start_element.getparent() # Option 1: Get all sibling elements (including the start element) all_siblings = list(parent_node) # Option 2: Get all siblings EXCEPT the start element siblings_excluding_start = [elem for elem in parent_node if elem is not start_element] # Option 3: Get only same-type siblings same_type_siblings = [elem for elem in parent_node if elem.tag == start_element.tag]
Each of these approaches gives you flexibility depending on your exact use case. Pick the one that fits how you're interacting with the XML!
内容的提问来源于stack exchange,提问作者Lju

