Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all nodes of a specific type in XPath

Lets say i have the following form data instance in my view.xml:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xmlns:exforms="http://www.exforms.org/exf/1-0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xhtml:head>
<xforms:instance id="instanceData">
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <fruits>
     <fruit>
        <fruit-name>Mango</fruit-name>
     </fruit>
     <fruit>
        <fruit-name>Apple</fruit-name>
     </fruit>
     <fruit>
        <fruit-name>Banana</fruit-name>
     </fruit>
</fruits>
</form>
</xforms:instance>
</xhtml:head>

I want to select all the fruit names from the above instance. I tried the following ways but it always selects the first fruit.

instance('instanceData')/fruits/fruit[*]/fruit-name
instance('instanceData')/fruits/fruit/fruit-name
instance('instanceData')/fruits/fruit[position()>0]/fruit-name

Please provide a way to overcome this in XPATH

like image 930
Thangamani J Avatar asked Mar 03 '11 11:03

Thangamani J


2 Answers

try this

             "//fruit-name"

It shall find all fruitnames wherever they are in the document hierarchy.

like image 96
Furqan Hameedi Avatar answered Sep 29 '22 12:09

Furqan Hameedi


If you want to select all the <fruit-name> from the instance instanceData (<xforms:instance id="instanceData">) that looks like the one you have in your question, the following should do it:

instance('instanceData')/fruits/fruit/fruit-name

If this doesn't work, one common reason is that you have a default namespace declaration in the document that contains your instance, like: xmlns="http://www.w3.org/1999/xhtml". If you have this, you need to undo that default namespace declaration on where you declare the instance, with:

<xforms:instance xmlns="" id="instanceData">

(And if this is the issue, my advice is not to use default namespace declarations. Ever. Instead declare xmlns:xhtml="http://www.w3.org/1999/xhtml" and use the xhtml prefix everywhere.)

like image 24
avernet Avatar answered Sep 29 '22 14:09

avernet