How to Use XMLPath?
XMLPath is a powerful XML query language that allows you to accurately select and extract data from XML documents, similar to XPath for HTML. It provides a flexible way to navigate and filter XML structures.
XML For You supports online XMLPath processing, just follow these steps:
- Paste the XML string into the left editor;
- Click "Search Command" at the top, find "Show XMLPath filter box", and an input box will appear at the bottom after clicking;
- Enter a XMLPath expression to execute it automatically;
- You can view the results in the right editor;
- When you need to edit multiple times, you can quickly swap the text in the left and right editors with the
Ctrl+Entershortcut;
XMLPath Syntax Basics
| Expression | Description | Example |
|---|---|---|
$ | Root object | $ |
@ | Current object | @ |
| Child property | $.name |
| Subscript operator | $[0] |
| Wildcard (matches all elements) | $.store.book[*] |
| Recursive descent | $..price |
| Filter expression | $..book[?(@.price < 10)] |
| Script expression | $..book[(@.length-1)] |
XMLPath Practical Examples
Example 1: Basic Property Access
XML data:
{
"name": "XML For You",
"features": ["formatting", "validation", "XMLPath"]
}
XMLPath expression: $.name, result: "XML For You"
Example 2: Array Indexing
XML data:
{
"users": [
{ "id": 1, "name": "John" },
{ "id": 2, "name": "Doe" }
]
}
XMLPath expression: $.users[1].name, result: "Doe"
Example 3: Conditional Filtering
XML data:
{
"products": [
{ "id": 1, "name": "Laptop", "price": 999 },
{ "id": 2, "name": "Mouse", "price": 25 },
{ "id": 3, "name": "Keyboard", "price": 50 }
]
}
XMLPath expression: $.products[?(@.price < 100)], result: returns the mouse and keyboard objects
Use Cases
- API Development: Extract specific fields from complex API responses
- Data Analysis: Filter and aggregate XML data for reports
- Configuration Management: Retrieve settings from nested XML configuration files
- Test Automation: Validate XML responses in API tests
- Log Analysis: Extract meaningful information from XML formatted logs