Re: Help with namespaces in xpath (PostgreSQL 9.5.3)

From: Allan Kamau <kamauallan(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Postgres General Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Help with namespaces in xpath (PostgreSQL 9.5.3)
Date: 2016-06-21 19:45:50
Message-ID: CAF3N6oSbcAF1L5RbYn+QVpzG=6_n8iEHTPMiwwkySqSAybOZiw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you David.

-Allan.

On Mon, Jun 20, 2016 at 11:19 PM, David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> On Sun, Jun 19, 2016 at 5:09 PM, Allan Kamau <kamauallan(at)gmail(dot)com> wrote:
>
>> I have an xml document from which I would like to extract the contents of
>> several elements.
>>
>> I would like to use xpath to extract the contents of "name" from the xml
>> document shown below.
>>
>> WITH x AS
>> (
>> SELECT
>> '<?xml version="1.0" encoding="UTF-8"?>
>> <uniprot xmlns="http://uniprot.org/uniprot" xmlns:xsi="
>> http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
>> http://uniprot.org/uniprot
>> http://www.uniprot.org/support/docs/uniprot.xsd">
>> <entry dataset="Swiss-Prot" created="2009-12-15" modified="2016-05-11"
>> version="56">
>> <accession>A0JM59</accession>
>> <name>UBP20_XENTR</name>
>> </entry>
>> </uniprot>
>> '::xml AS d
>> )
>> SELECT (xpath('/uniprot/entry/name/text()',a.d))[1]::text AS uniprot_name
>> FROM
>> x AS a
>> ;
>>
>> The documentation for xpath() ("
>> https://www.postgresql.org/docs/9.5/static/functions-xml.html")
>> describes "xpath(xpath, xml [, nsarray]").
>>
>> For the above xml document, what would be the two dimensional array
>> "nsarray" for the xpath() function?
>>
>
> ​Is there a specific part of the description and two examples that doesn't
> make sense to you?
>
> ​Or more specifically, do you understand what namespaces are?​
>
> ARRAY[
> ARRAY['defaultns','http://uniprot.org/uniprot'],
> ARRAY['xsi','http://www.w3.org/2001/XMLSchema-instance']
> ]​
>
> In effect when the xpath function parses the XML document it tosses away
> all of the document-local namespace aliases and instead associated the full
> namespace URI with each element (in the DOM). Since, in the xpath
> expression, usually you'd want to refer to nodes in the DOM via their
> namespace alias you need to tell the xpath function which aliases you
> intend to use in the xpath and which full URI they correspond to.
> Furthermore, there is not concept of a default namespace in the xpath
> expression. So while you can simply copy-paste the aliases and URIs from
> all of the non-default namespace aliases you must also choose a unique
> alias for the default namespace in the original document.
>
> In the above I've copied the alias and namespace URI for the named "xsi"
> alias and gave the name "defaultns" to the original document's default
> namespace URI.
>
> David J.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jonathan Vanasco 2016-06-21 20:39:59 does timestamp precision affect storage size?
Previous Message Ramalingam, Sankarakumar 2016-06-21 19:34:18 Help on recovering my standby