Debian Codegraph @

contrib/xml2/sql/xml2.sql

raw
1 CREATE EXTENSION xml2;
2
3 select query_to_xml('select 1 as x',true,false,'');
4
5 select xslt_process( query_to_xml('select x from generate_series(1,5) as
6 x',true,false,'')::text,
7 $$<xsl:stylesheet version="1.0"
8 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
9 <xsl:output method="xml" indent="yes" />
10 <xsl:template match="*">
11 <xsl:copy>
12 <xsl:copy-of select="@*" />
13 <xsl:apply-templates />
14 </xsl:copy>
15 </xsl:template>
16 <xsl:template match="comment()|processing-instruction()">
17 <xsl:copy />
18 </xsl:template>
19 </xsl:stylesheet>
20 $$::text);
21
22 CREATE TABLE xpath_test (id integer NOT NULL, t xml);
23 INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
24 SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
25 as t(id int4);
26 SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
27 as t(id int4, doc int4);
28
29 DROP TABLE xpath_test;
30 CREATE TABLE xpath_test (id integer NOT NULL, t text);
31 INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
32 SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
33 as t(id int4);
34 SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
35 as t(id int4, doc int4);
36
37 create table articles (article_id integer, article_xml xml, date_entered date);
38 insert into articles (article_id, article_xml, date_entered)
39 values (2, '<article><author>test</author><pages>37</pages></article>', now());
40 SELECT * FROM
41 xpath_table('article_id',
42 'article_xml',
43 'articles',
44 '/article/author|/article/pages|/article/title',
45 'date_entered > ''2003-01-01'' ')
46 AS t(article_id integer, author text, page_count integer, title text);
47
48 -- this used to fail when invoked a second time
49 select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
50 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
51 <xsl:template match="@*|node()">
52 <xsl:copy>
53 <xsl:apply-templates select="@*|node()"/>
54 </xsl:copy>
55 </xsl:template>
56 </xsl:stylesheet>$$)::xml;
57
58 select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
59 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
60 <xsl:template match="@*|node()">
61 <xsl:copy>
62 <xsl:apply-templates select="@*|node()"/>
63 </xsl:copy>
64 </xsl:template>
65 </xsl:stylesheet>$$)::xml;
66
67 create table t1 (id integer, xml_data xml);
68 insert into t1 (id, xml_data)
69 values
70 (1, '<attributes><attribute name="attr_1">Some
71 Value</attribute></attributes>');
72
73 create index idx_xpath on t1 ( xpath_string
74 ('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));
75
76 SELECT xslt_process('<employee><name>cim</name><age>30</age><pay>400</pay></employee>'::text, $$<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
77 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
78 <xsl:strip-space elements="*"/>
79 <xsl:param name="n1"/>
80 <xsl:param name="n2"/>
81 <xsl:param name="n3"/>
82 <xsl:param name="n4"/>
83 <xsl:param name="n5" select="'me'"/>
84 <xsl:template match="*">
85 <xsl:element name="samples">
86 <xsl:element name="sample">
87 <xsl:value-of select="$n1"/>
88 </xsl:element>
89 <xsl:element name="sample">
90 <xsl:value-of select="$n2"/>
91 </xsl:element>
92 <xsl:element name="sample">
93 <xsl:value-of select="$n3"/>
94 </xsl:element>
95 <xsl:element name="sample">
96 <xsl:value-of select="$n4"/>
97 </xsl:element>
98 <xsl:element name="sample">
99 <xsl:value-of select="$n5"/>
100 </xsl:element>
101 <xsl:element name="sample">
102 <xsl:value-of select="$n6"/>
103 </xsl:element>
104 <xsl:element name="sample">
105 <xsl:value-of select="$n7"/>
106 </xsl:element>
107 <xsl:element name="sample">
108 <xsl:value-of select="$n8"/>
109 </xsl:element>
110 <xsl:element name="sample">
111 <xsl:value-of select="$n9"/>
112 </xsl:element>
113 <xsl:element name="sample">
114 <xsl:value-of select="$n10"/>
115 </xsl:element>
116 <xsl:element name="sample">
117 <xsl:value-of select="$n11"/>
118 </xsl:element>
119 <xsl:element name="sample">
120 <xsl:value-of select="$n12"/>
121 </xsl:element>
122 </xsl:element>
123 </xsl:template>
124 </xsl:stylesheet>$$::text, 'n1="v1",n2="v2",n3="v3",n4="v4",n5="v5",n6="v6",n7="v7",n8="v8",n9="v9",n10="v10",n11="v11",n12="v12"'::text);
125
126 -- xpath_nodeset()
127 SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages')
128 FROM articles;
129 SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
130 'item_without_toptag')
131 FROM articles;
132 SELECT xpath_nodeset(article_xml::text, '/article/author|/article/pages',
133 'result', 'item')
134 FROM articles;
135 -- namespace node
136 SELECT xpath_nodeset('<root xmlns:foo="http://icl.com/saxon"/>',
137 '//namespace::foo');
138
139 -- xpath_list()
140 SELECT xpath_list(article_xml::text, '/article/author|/article/pages')
141 FROM articles;
142 SELECT xpath_list(article_xml::text, '/article/author|/article/pages', '|')
143 FROM articles;
144
145 -- possible security exploit
146 SELECT xslt_process('<xml><foo>Hello from XML</foo></xml>',
147 $$<xsl:stylesheet version="1.0"
148 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
149 xmlns:sax="http://icl.com/saxon"
150 extension-element-prefixes="sax">
151
152 <xsl:template match="//foo">
153 <sax:output href="0wn3d.txt" method="text">
154 <xsl:value-of select="'0wn3d via xml2 extension and libxslt'"/>
155 <xsl:apply-templates/>
156 </sax:output>
157 </xsl:template>
158 </xsl:stylesheet>$$);
159
160 -- empty output
161 select xslt_process('<aaa/>',
162 $$<xsl:stylesheet version="1.0"
163 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
164 </xsl:stylesheet>$$);