3

I have a database in Postgresql where I put some documents written in xml. I want to search twrough them using XPath, but my code is not working. The document are like this:

<?xml version="1.0" standalone="yes"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <teiHeader>
        ....
        <revisionDesc>
            <listChange>
                <change when="2017-01-11+01:00" who="person"/>
            </listChange>
        </revisionDesc>
    </teiHeader>
    ...
</TEI>

I'm trying to get the when attribute of the change element. My code so far:

SELECT 

xpath('TEI/n:teiHeader/n:revisionDesc/n:listChange/n:change[@when]/text()',

xml_document , '{{n,http://www.tei-c.org/ns/1.0}}') 

FROM xml_table where xml_id = 5;

It gives me empty result like this: {} and I don't understand why

6
  • 1
    1. TEI element is also in default namespace. Use prefix for it as well : n:TEI/...... 2. change element doesn't have inner text so /text() won't find anything, and I don't know what you actually want to get from change element Commented Jan 13, 2017 at 12:16
  • The value of the when attribute, in the example 2017-01-11+01:00 Commented Jan 13, 2017 at 12:52
  • In that case, replace /text() with /@when Commented Jan 13, 2017 at 12:55
  • 'n:TEI/n:teiHeader/n:revisionDesc/n:listChange/n:change/@when' and 'n:TEI/n:teiHeader/n:revisionDesc/n:listChange/n:change[@when]/@when' give the same result {}, did I miss something? Commented Jan 13, 2017 at 13:17
  • 1
    @tina prepend them with / to denote that n:TEI is you root element -- or ommit n:TEI completely, because xpath() will search for results relative to the root element (in xpath terms, that is the current node). Commented Jan 13, 2017 at 14:13

1 Answer 1

1

You need to prepend your xpath query with / if you want to reference the root element. Or, you could ommit n:TEI, because the xpath() function will search for results relative to the root element (in xpath terms, that is the current node).

I.e. these xpath queries will find the when attributes' values:

/n:TEI/n:teiHeader/n:revisionDesc/n:listChange/n:change/@when
n:teiHeader/n:revisionDesc/n:listChange/n:change/@when
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.