Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 May PostgreSQL XML Type

PostgreSQL XML Type

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on the XML data type of PostgreSQL.

Explain with the sample example.

What is XML Type ?

The XML Data type is used to store XML data. XML is well-formed storage structure of data. You can store multiple nodes into a single XML column. You can store data in hierarchical format.

E.g,

User preferences table, in which User can select multiple preferences and even User can create custom choices for them.

Now, how you can store this preferences data for those Users.

Simple, create one XML column and store all User preferences id into this. like,

<preferences><id>1</id><id>2</id><id>4</id></preferences>.

Below is a demonstration of XML with PostgreSQL.

Basically you can store XML data with one child node or with different child node.

Les’s First start with one child node:
Create table with XML type:

1
CREATE TABLE XMLTEST (Name XML);

Now Insert XML Data.

1
2
3
4
5
6
7
8
9
10
INSERT INTO XMLTEST
VALUES
('
abc
xyz
pqr
cdf
');

Select data from XMLTEST:

1
SELECT *FROM XMLTEST

You can find above full XML into one column.

XMLPostgresResultOne

Now, time to parse this XML column using unnest() function of PostgreSQL.

1
SELECT unnest(xpath('.//Sub/text()', Name::xml))::text AS XMLDATA FROM XMLTEST;

Result is:

XMLPostgresResultTwo

You can see this is four rows now, as we have stored different four values into one XML Column and after parse, we can see those four values in the form of different row.

Demonstration of different child node:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
INSERT INTO XMLTEST VALUES
('
Anvesh
Hyderabad
Male
27
');
 
SELECT
unnest(xpath('//Name/text()', Name::XML)) AS Name
,unnest(xpath('//City/text()', Name::XML)) AS City
,unnest(xpath('//Gender/text()', Name::XML)) AS Gender
,unnest(xpath('//Age/text()', Name::XML)) AS Age
FROM XMLTEST;

Result is :

XMLPostgresResultThree

In the above code, we inserted an XML data with different child nodes.

You can see the result of one row with the various columns for the different child nodes.

May 22, 2015Anvesh Patel
MySQL Error HandlingPostgreSQL [Video]: Cross Database Queries using DbLink Extension
Comments: 5
  1. Maria
    September 16, 2015 at 5:02 pm

    Thanks Anvesh,
    good article !
    Really You solved my problem for postgres XML.

  2. hagai
    May 5, 2016 at 5:45 am

    can you give an example of a query that involve where clause in the xpath ?

    • Anvesh Patel
      Anvesh Patel
      May 13, 2016 at 9:21 am

      ;WITH cteXML
      AS
      (
      SELECT
      CAST(unnest(xpath(‘//Name/text()’, Name::XML)) AS TEXT) AS Name
      ,CAST(unnest(xpath(‘//City/text()’, Name::XML)) AS TEXT) AS City
      ,CAST(unnest(xpath(‘//Gender/text()’, Name::XML)) AS TEXT) AS Gender
      ,CAST(unnest(xpath(‘//Age/text()’, Name::XML)) AS TEXT) AS Age
      FROM XMLTEST
      )
      SELECT *FROM cteXML
      WHERE Name=’Anvesh’;

  3. ashwin
    March 16, 2018 at 1:07 pm

    can u please give the simple example of update and delete querry for the same

  4. Daniel
    February 17, 2020 at 12:10 pm

    Hi Anvesh,

    Am getting this Error ,

    DETAIL: This functionality requires the server to be built with libxml support.
    HINT: You need to rebuild PostgreSQL using –with-libxml.

    How to resolve.

Anvesh Patel
Anvesh Patel

Database Engineer

May 22, 2015 PostgreSQLAnvesh Patel, database, database research and development, datatype, node, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Programming, PostgreSQL Tips and Tricks, unnest, XML
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....