1CREATE EXTENSION xml2;
2
3select query_to_xml('select 1 as x',true,false,'');
4
5select xslt_process( query_to_xml('select x from generate_series(1,5) as
6x',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
22CREATE TABLE xpath_test (id integer NOT NULL, t xml);
23INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
24SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
25as t(id int4);
26SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
27as t(id int4, doc int4);
28
29DROP TABLE xpath_test;
30CREATE TABLE xpath_test (id integer NOT NULL, t text);
31INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
32SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
33as t(id int4);
34SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
35as t(id int4, doc int4);
36
37create table articles (article_id integer, article_xml xml, date_entered date);
38insert into articles (article_id, article_xml, date_entered)
39values (2, '<article><author>test</author><pages>37</pages></article>', now());
40SELECT * FROM
41xpath_table('article_id',
42            'article_xml',
43            'articles',
44            '/article/author|/article/pages|/article/title',
45            'date_entered > ''2003-01-01'' ')
46AS t(article_id integer, author text, page_count integer, title text);
47
48-- this used to fail when invoked a second time
49select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
50xmlns: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
58select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
59xmlns: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
67create table t1 (id integer, xml_data xml);
68insert into t1 (id, xml_data)
69values
70(1, '<attributes><attribute name="attr_1">Some
71Value</attribute></attributes>');
72
73create index idx_xpath on t1 ( xpath_string
74('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));
75
76SELECT 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-- possible security exploit
127SELECT xslt_process('<xml><foo>Hello from XML</foo></xml>',
128$$<xsl:stylesheet version="1.0"
129      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
130      xmlns:sax="http://icl.com/saxon"
131      extension-element-prefixes="sax">
132
133  <xsl:template match="//foo">
134    <sax:output href="0wn3d.txt" method="text">
135      <xsl:value-of select="'0wn3d via xml2 extension and libxslt'"/>
136      <xsl:apply-templates/>
137    </sax:output>
138  </xsl:template>
139</xsl:stylesheet>$$);
140