xpath3
Written as xpath3(argnum flags):options in a transform expression.
| Minimum arguments | 1 |
| Maximum arguments | 1 |
| Default arguments | 1 |
| Argument modes | SINGLE_PRIMITIVE |
Documentation
Section titled “Documentation”Extract data from XML/HTML content using XPath3 expressions.
Features:
- Supports standard XPath 3.0/3.1 syntax
- Works with both XML and HTML content
- Automatic recovery (with ‘RECOVER XML’ flag) of broken XML. However, use this flag with caution, as this process may not fully recover the original XML document structure when ambiguilty exists.
- Namespace support:
- Enabled by default. Can be disabled with ‘IGNORE NAMESPACE’ flag. When ‘IGNORE NAMESPACE’ flag is turned on, namespace mapping is ignored, and the transform’s options data is interpretted as the xpath expression
- [Optional] Namespace mapping via a preliminary mapping dict before the actual xpath
expression, such as
{"ns1":"http://mymodel.org/model1/1.0"}||//main//model1
The XPath3 expression (from the transform’s options data) supports 2 forms:
- {namespace_mapping}||{xpath_expr}: e.g. {"ns1":"http://mymodel.org/model1/1.0"}||//main//ns1:model1
- {xpath_expr}: e.g. //main//model1
where `{namespace_mapping}` is the namespace mapping dict and `{xpath_expr}` is the actual xpath expression.| Flag | Letter |
|---|---|
| spread_output | S |
| noncaptive_input | I |
| recover_xml | r |
| IGNORE_NAMESPACE | N |
Examples
Section titled “Examples”Extract all links from HTML
Section titled “Extract all links from HTML”Input args: ["<html><body><a href=\"link1\">Link 1</a><a href=\"link2\">Link 2</a></body></html>"]Transforms: "xpath3://a/@href"Output: ["link1","link2"]Extract text content
Section titled “Extract text content”Input args: ["<root><item>First</item><item>Second</item></root>"]Transforms: "xpath3://item/text()"Output: ["First","Second"]Extract attribute value
Section titled “Extract attribute value”Input args: ["<div id=\"main\" class=\"container\"></div>"]Transforms: "xpath3://div/@class"Output: ["container"]Use xpath3 built-in functions
Section titled “Use xpath3 built-in functions”Input args: ["<root/>"]Transforms: "xpath3:math:atan(1.0e0)"Output: 0.7853981633974483Use xpath3 “let” expression
Section titled “Use xpath3 “let” expression”Input args: ["<root/>"]Transforms: "xpath3:let $x := 5, $y := 10 return $x + $y"Output: 15Use xpath3 conditional expression
Section titled “Use xpath3 conditional expression”Input args: ["<root/>"]Transforms: "xpath3:if (//item) then 'has items' else 'no items'"Output: ["no items"]Use xpath3 sequence construction
Section titled “Use xpath3 sequence construction”Input args: ["<root/>"]Transforms: "xpath3:(1 to 5)"Output: [1,2,3,4,5]Use xpath3 higher order functions
Section titled “Use xpath3 higher order functions”Input args: ["<root/>"]Transforms: "xpath3:let $f := function($x) { $x * 2 } return $f(5)"Output: 10Use xpath3 string-join functions
Section titled “Use xpath3 string-join functions”Input args: ["<root/>"]Transforms: "xpath3:string-join(('a','b','c'), '-')"Output: "a-b-c"Use xpath3 array/map construction
Section titled “Use xpath3 array/map construction”Input args: ["<root/>"]Transforms: "xpath3:array { 1, 2, 3 }, map { 'a': 1, 'b': 2 }"Output: [[1,2,3],{"a":1,"b":2}]Use xpath3 quantified expressions
Section titled “Use xpath3 quantified expressions”Input args: ["<root/>"]Transforms: "xpath3:some $x in (1,2,3) satisfies $x > 2"Output: trueUse xpath3 arrow operator
Section titled “Use xpath3 arrow operator”Input args: ["<root/>"]Transforms: "xpath3:('a','b','c') => count()"Output: 3Recover slightly malformed xml
Section titled “Recover slightly malformed xml”Input args: ["<Parent><Child1/><Child2></Parent>"]Transforms: "xpath3(r):/Parent/*"Output: ["<Child1/>","<Child2/>"]Use namespaced xml
Section titled “Use namespaced xml”Input args: ["<Parent xmlns=\"http://testxml.org/parent/1.0\"><Child1/><Child2/></Parent>"]Transforms: "xpath3(r):{\"ns1\":\"http://testxml.org/parent/1.0\"}||/ns1:Parent/*"Output: ["<Child1 xmlns=\"http://testxml.org/parent/1.0\"/>","<Child2 xmlns=\"http://testxml.org/parent/1.0\"/>"]Ignore namespaces in xml
Section titled “Ignore namespaces in xml”Input args: ["<Parent xmlns=\"http://testxml.org/parent/1.0\"><Child1/><Child2/></Parent>"]Transforms: "xpath3(N):/Parent/*"Output: ["<Child1 xmlns=\"http://testxml.org/parent/1.0\"/>","<Child2 xmlns=\"http://testxml.org/parent/1.0\"/>"]