xml2
XPath querying and XSLT
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
xml2 | 1.1 | TYPE | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3990 | xml2 | No | No | No | Yes | No | No | - |
| Related | pgjq prefix semver unit pgpdf pglite_fusion md5hash asn1oid |
|---|
Version
| PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|
| 1.1 | 1.1 | 1.1 | 1.1 | 1.1 |
Install
Note: This is a built-in contrib extension of PostgreSQL
CREATE EXTENSION xml2;
Usage
The xml2 extension provides XPath querying and XSLT transformation functions for XML documents. Note: this module is deprecated in favor of the core SQL/XML functionality.
CREATE EXTENSION xml2;
XML Validation
SELECT xml_valid('<doc><item>test</item></doc>'); -- true
XPath Query Functions
-- Extract text value
SELECT xpath_string('<doc><name>Alice</name></doc>', '/doc/name');
-- Extract numeric value
SELECT xpath_number('<doc><count>42</count></doc>', '/doc/count');
-- Extract boolean
SELECT xpath_bool('<doc><active>true</active></doc>', '/doc/active');
-- Extract node set with tags
SELECT xpath_nodeset('<doc><a>1</a><a>2</a></doc>', '/doc/a', 'results', 'item');
-- Extract values as comma-separated list
SELECT xpath_list('<doc><a>1</a><a>2</a></doc>', '/doc/a'); -- 1,2
xpath_table Function
Evaluate multiple XPath queries across a set of documents:
SELECT * FROM
xpath_table('id', 'xml_col', 'documents',
'/doc/author|/doc/title',
'true')
AS t(id int, author text, title text);
Parameters:
- Key field name (first output column)
- XML document field name
- Table/view name
- XPath expressions separated by
| - WHERE clause (use
'true'for all rows)
XSLT Transformation
-- Apply XSL stylesheet to document
SELECT xslt_process(xml_document, xsl_stylesheet);
-- With parameters
SELECT xslt_process(xml_document, xsl_stylesheet, 'param1=value1,param2=value2');
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.