How to find all nodes of a specific type in XPath

Thangamani J picture Thangamani J · Mar 3, 2011 · Viewed 10.8k times · Source

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

Answer

Furqan Hameedi picture Furqan Hameedi · Mar 3, 2011

try this

             "//fruit-name"

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