Processing XML
-
-
-
To process values of data type xml, PostgreSQL offers
the functions xpath and
expressions.
+
+
xpath
+
+
+
+
xpath(xpath, xml , nsarray)
- The function xpath evaluates the XPath
- expression xpath (a text> value)
- against the XML value
- xml. It returns an array of XML values
- corresponding to the node set produced by the XPath expression.
- If the XPath expression returns a scalar value rather than a node set,
- a single-element array is returned.
-
+ The function xpath evaluates the XPath
+ expression xpath (a text> value)
+ against the XML value
+ xml. It returns an array of XML values
+ corresponding to the node set produced by the XPath expression.
+ If the XPath expression returns a scalar value rather than a node set,
+ a single-element array is returned.
+
- The second argument must be a well formed XML document. In particular,
- it must have a single root node element.
-
+ The second argument must be a well formed XML document. In particular,
+ it must have a single root node element.
+
- The optional third argument of the function is an array of namespace
- mappings. This array should be a two-dimensional text> array with
- the length of the second axis being equal to 2 (i.e., it should be an
- array of arrays, each of which consists of exactly 2 elements).
- The first element of each array entry is the namespace name (alias), the
- second the namespace URI. It is not required that aliases provided in
- this array be the same as those being used in the XML document itself (in
- other words, both in the XML document and in the xpath
- function context, aliases are local>).
-
+ The optional third argument of the function is an array of namespace
+ mappings. This array should be a two-dimensional text> array with
+ the length of the second axis being equal to 2 (i.e., it should be an
+ array of arrays, each of which consists of exactly 2 elements).
+ The first element of each array entry is the namespace name (alias), the
+ second the namespace URI. It is not required that aliases provided in
+ this array be the same as those being used in the XML document itself (in
+ other words, both in the XML document and in the xpath
+ function context, aliases are local>).
+
- Example:
+ Example:
SELECT xpath('/my:a/text()', 'test',
ARRAY[ARRAY['my', 'http://example.com']]);
{test}
(1 row)
]]>
-
+
- To deal with default (anonymous) namespaces, do something like this:
+ To deal with default (anonymous) namespaces, do something like this:
SELECT xpath('//mydefns:b/text()', '
test',
ARRAY[ARRAY['mydefns', 'http://example.com']]);
{test}
(1 row)
]]>
-
+
+
-
-
+
+
xpath_exists
+
+
+
xpath_exists(xpath, xml , nsarray)
- The function xpath_exists is a specialized form
- of the xpath function. Instead of returning the
- individual XML values that satisfy the XPath, this function returns a
- Boolean indicating whether the query was satisfied or not. This
- function is equivalent to the standard XMLEXISTS> predicate,
- except that it also offers support for a namespace mapping argument.
-
+ The function xpath_exists is a specialized form
+ of the xpath function. Instead of returning the
+ individual XML values that satisfy the XPath, this function returns a
+ Boolean indicating whether the query was satisfied or not. This
+ function is equivalent to the standard XMLEXISTS> predicate,
+ except that it also offers support for a namespace mapping argument.
+
- Example:
+ Example:
SELECT xpath_exists('/my:a/text()', 'test',
ARRAY[ARRAY['my', 'http://example.com']]);
t
(1 row)
]]>
-
+
+