]> granicus.if.org Git - postgresql/blob - contrib/xml2/README.xml2
Add blank line.
[postgresql] / contrib / xml2 / README.xml2
1 XML-handling functions for PostgreSQL
2 =====================================
3
4 Development of this module was sponsored by Torchbox Ltd. (www.torchbox.com)
5 It has the same BSD licence as PostgreSQL.
6
7 This version of the XML functions provides both XPath querying and
8 XSLT functionality. There is also a new table function which allows
9 the straightforward return of multiple XML results. Note that the current code
10 doesn't take any particular care over character sets - this is
11 something that should be fixed at some point!
12
13 Installation
14 ------------
15
16 The current build process will only work if the files are in
17 contrib/xml2 in a PostgreSQL 7.3 or later source tree which has been
18 configured and built (If you alter the subdir value in the Makefile
19 you can place it in a different directory in a PostgreSQL tree).
20
21 Before you begin, just check the Makefile, and then just 'make' and
22 'make install'.
23
24 This code requires libxml to be previously installed.
25
26 Description of functions
27 ------------------------
28
29 The first set of functions are straightforward XML parsing and XPath queries:
30
31 xml_valid(document) RETURNS bool
32
33 This parses the document text in its parameter and returns true if the
34 document is well-formed XML.
35
36 xpath_string(document,query) RETURNS text
37 xpath_number(document,query) RETURNS float4
38 xpath_bool(document,query) RETURNS bool
39
40 These functions evaluate the XPath query on the supplied document, and
41 cast the result to the specified type.
42
43
44 xpath_nodeset(document,query,toptag,itemtag) RETURNS text
45
46 This evaluates query on document and wraps the result in XML tags. If
47 the result is multivalued, the output will look like:
48
49 <toptag>
50 <itemtag>Value 1 which could be an XML fragment</itemtag>
51 <itemtag>Value 2....</itemtag>
52 </toptag>
53
54 If either toptag or itemtag is an empty string, the relevant tag is omitted.
55 There are also wrapper functions for this operation:
56
57 xpath_nodeset(document,query) RETURNS text omits both tags.
58 xpath_nodeset(document,query,itemtag) RETURNS text omits toptag.
59
60
61 xpath_list(document,query,seperator) RETURNS text
62
63 This function returns multiple values seperated by the specified
64 seperator, e.g. Value 1,Value 2,Value 3 if seperator=','.
65
66 xpath_list(document,query) RETURNS text
67
68 This is a wrapper for the above function that uses ',' as the seperator.
69
70
71 xpath_table
72 -----------
73
74 This is a table function which evaluates a set of XPath queries on
75 each of a set of documents and returns the results as a table. The
76 primary key field from the original document table is returned as the
77 first column of the result so that the resultset from xpath_table can
78 be readily used in joins.
79
80 The function itself takes 5 arguments, all text.
81
82 xpath_table(key,document,relation,xpaths,criteria)
83
84 key - the name of the "key" field - this is just a field to be used as
85 the first column of the output table i.e. it identifies the record from
86 which each output row came (see note below about multiple values).
87
88 document - the name of the field containing the XML document
89
90 relation - the name of the table or view containing the documents
91
92 xpaths - multiple xpath expressions separated by |
93
94 criteria - The contents of the where clause. This needs to be specified,
95 so use "true" or "1=1" here if you want to process all the rows in the
96 relation.
97
98 NB These parameters (except the XPath strings) are just substituted
99 into a plain SQL SELECT statement, so you have some flexibility - the
100 statement is
101
102 SELECT <key>,<document> FROM <relation> WHERE <criteria>
103
104 so those parameters can be *anything* valid in those particular
105 locations. The result from this SELECT needs to return exactly two
106 columns (which it will unless you try to list multiple fields for key
107 or document). Beware that this simplistic approach requires that you
108 validate any user-supplied values to avoid SQL injection attacks.
109
110 Using the function
111
112 The function has to be used in a FROM expression. This gives the following
113 form:
114
115 SELECT * FROM
116 xpath_table('article_id', 
117         'article_xml',
118         'articles', 
119         '/article/author|/article/pages|/article/title',
120         'date_entered > ''2003-01-01'' ') 
121 AS t(article_id integer, author text, page_count integer, title text);
122
123 The AS clause defines the names and types of the columns in the
124 virtual table. If there are more XPath queries than result columns,
125 the extra queries will be ignored. If there are more result columns
126 than XPath queries, the extra columns will be NULL.
127
128 Note that I've said in this example that pages is an integer.  The
129 function deals internally with string representations, so when you say
130 you want an integer in the output, it will take the string
131 representation of the XPath result and use PostgreSQL input functions
132 to transform it into an integer (or whatever type the AS clause
133 requests). An error will result if it can't do this - for example if
134 the result is empty - so you may wish to just stick to 'text' as the
135 column type if you think your data has any problems.
136
137 The select statement doesn't need to use * alone - it can reference the
138 columns by name or join them to other tables. The function produces a
139 virtual table with which you can perform any operation you wish (e.g.
140 aggregation, joining, sorting etc). So we could also have:
141
142 SELECT t.title, p.fullname, p.email 
143 FROM xpath_table('article_id','article_xml','articles',
144             '/article/title|/article/author/@id',
145             'xpath_string(article_xml,''/article/@date'') > ''2003-03-20'' ')
146             AS t(article_id integer, title text, author_id integer), 
147      tblPeopleInfo AS p 
148 WHERE t.author_id = p.person_id;
149
150 as a more complicated example. Of course, you could wrap all
151 of this in a view for convenience.
152
153 Multivalued results
154
155 The xpath_table function assumes that the results of each XPath query
156 might be multi-valued, so the number of rows returned by the function
157 may not be the same as the number of input documents. The first row
158 returned contains the first result from each query, the second row the
159 second result from each query. If one of the queries has fewer values
160 than the others, NULLs will be returned instead.
161
162 In some cases, a user will know that a given XPath query will return
163 only a single result (perhaps a unique document identifier) - if used
164 alongside an XPath query returning multiple results, the single-valued
165 result will appear only on the first row of the result. The solution
166 to this is to use the key field as part of a join against a simpler
167 XPath query. As an example:
168
169
170 CREATE TABLE test
171 (
172   id int4 NOT NULL,
173   xml text,
174   CONSTRAINT pk PRIMARY KEY (id)
175
176 WITHOUT OIDS;
177
178 INSERT INTO test VALUES (1, '<doc num="C1">
179 <line num="L1"><a>1</a><b>2</b><c>3</c></line>
180 <line num="L2"><a>11</a><b>22</b><c>33</c></line>
181 </doc>');
182
183 INSERT INTO test VALUES (2, '<doc num="C2">
184 <line num="L1"><a>111</a><b>222</b><c>333</c></line>
185 <line num="L2"><a>111</a><b>222</b><c>333</c></line>
186 </doc>');
187
188
189 The query:
190
191 SELECT * FROM  xpath_table('id','xml','test', 
192 '/doc/@num|/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c','1=1') 
193 AS t(id int4, doc_num varchar(10), line_num varchar(10), val1 int4, 
194 val2 int4, val3 int4)
195 WHERE id = 1 ORDER BY doc_num, line_num
196
197
198 Gives the result:
199
200  id | doc_num | line_num | val1 | val2 | val3
201 ----+---------+----------+------+------+------
202   1 | C1      | L1       |    1 |    2 |    3
203   1 |         | L2       |   11 |   22 |   33
204
205 To get doc_num on every line, the solution is to use two invocations
206 of xpath_table and join the results:
207
208 SELECT t.*,i.doc_num FROM 
209   xpath_table('id','xml','test',
210    '/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c','1=1') 
211         AS t(id int4, line_num varchar(10), val1 int4, val2 int4, val3 int4),
212   xpath_table('id','xml','test','/doc/@num','1=1') 
213         AS i(id int4, doc_num varchar(10))
214 WHERE i.id=t.id AND i.id=1
215 ORDER BY doc_num, line_num;
216
217 which gives the desired result:
218
219  id | line_num | val1 | val2 | val3 | doc_num
220 ----+----------+------+------+------+---------
221   1 | L1       |    1 |    2 |    3 | C1
222   1 | L2       |   11 |   22 |   33 | C1
223 (2 rows)
224
225
226
227 XSLT functions
228 --------------
229
230 The following functions are available if libxslt is installed (this is
231 not currently detected automatically, so you will have to amend the
232 Makefile)
233
234 xslt_process(document,stylesheet,paramlist) RETURNS text
235
236 This function appplies the XSL stylesheet to the document and returns
237 the transformed result. The paramlist is a list of parameter
238 assignments to be used in the transformation, specified in the form
239 'a=1,b=2'. Note that this is also proof-of-concept code and the
240 parameter parsing is very simple-minded (e.g. parameter values cannot
241 contain commas!)
242
243 Also note that if either the document or stylesheet values do not
244 begin with a < then they will be treated as URLs and libxslt will
245 fetch them. It thus follows that you can use xslt_process as a means
246 to fetch the contents of URLs - you should be aware of the security
247 implications of this.
248
249 There is also a two-parameter version of xslt_process which does not
250 pass any parameters to the transformation.
251
252
253 Feedback
254 --------
255
256 If you have any comments or suggestions, please do contact me at
257 jgray@azuli.co.uk. Unfortunately, this isn't my main job, so I can't
258 guarantee a rapid response to your query!