question

drew avatar image
drew asked

Help How Will I get Multivalued results using xpath_table functions? see my example

Im uploading an xml file in our database but i have encountered a problem I can't get multiple phone number, under child relationship of our file.

Here is my example:

CREATE TABLE test (
        id int4 NOT NULL,
        xml text,
        CONSTRAINT pk PRIMARY KEY (id)
    );

INSERT INTO test VALUES (1, 
'<Agency><Key>A123</Key>
  <Site>
    <Key>S111</Key>
    <Phone><Number>111-3456</Number><Type>Main</Type></Phone>
    <Phone><Number>111-4567</Number><Type>Fax</Type></Phone>
    <Phone><Number>111-4567</Number><Type>Voice</Type></Phone>
  </Site>
  <Site>
    <Key>T222</Key>
    <Phone><Number>222-5647</Number><Type>Main</Type></Phone>
    <Phone><Number>222-9874</Number><Type>Fax</Type></Phone>
  </Site>
  <Site>
    <Key>R333</Key>
    <Phone><Number>222-5468</Number><Type>Main</Type></Phone>
    <Phone><Number>222-2233</Number><Type>Fax</Type></Phone>
  </Site>
</Agency>');


SELECT * FROM
      xpath_table('id','xml','test','
/Agency/Key|/Agency/Site/Key|/Agency/Site/Phone/Number|/Agency/Site/Phone/Type','true')
as
t(id integer,agencykey text, sitekey text, phonenumber text, phonetype text);




id | agencykey | sitekey | phonenumber | phonetype 
---+-----------+---------+-------------+-----------
 1 | A123      | S111    | 111-3456    | Main  
 1 |           | T222    | 111-4567    | Fax
 1 |           | R333    | 111-4567    | Voice
 1 |           |         | 222-5647    | Main
 1 |           |         | 222-9874    | Fax
 1 |           |         | 222-5468    | Main
 1 |           |         | 222-2233    | Fax
(7 rows)

How will I assign the sitekey to phonenumbers?

Im using PostgreSQL 8.2.11

Many Thanks,

xmlfunctionspostgresql
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
I have closed this as off topic, seeing as xpath_table is Postgres specific, and this is a SQL Server site
0 Likes 0 ·
sp_lock avatar image sp_lock commented ·
@Matt, I would have thought that "Im using PostgreSQL 8.2.11" gave it away. ;-)
0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.