xml2

xml2

xml2 : XPath querying and XSLT

Overview

ID Extension Package Version Category License Language
3990
xml2
xml2
1.1
TYPE
PostgreSQL
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
----d--
No
No
No
Yes
no
no
Relationships
See Also
pgjq
prefix
semver
unit
pgpdf
pglite_fusion
md5hash
asn1oid

Packages

PG18 PG17 PG16 PG15 PG14
1.1
1.1
1.1
1.1
1.1

This is a built-in contrib extension ship with the PostgreSQL kernel

Install

Create this extension with:

CREATE EXTENSION xml2;

Usage

xml2: XPath querying and XSLT functionality

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');
Last updated on