Search code examples
xmlxpathxqueryxpath-2.0berkeley-db-xml

Oracle - Berkeley DB XML Java API - XML query to get attribute values at multiple level


Evaluating Berkeley DB with Java APIs. Following is one of my test XML data:

<master>
    <env name="development">
        <server name="tomcat1" ip="122.122.123.1">
            <domain name="domain1">
                <application name="GreatApplication1" status="enabled"/>
            </domain>
            <domain name="domain2">
                <application name="GreatApplication2" status="enabled"/>
                <application name="NotSoGreatApplication2" status="disabled"/>
                <application name="GreatApplication3" status="enabled"/>
            </domain>
        </server>
    </env>
</master>

With following query String, I can query applications and their status on any "domain" for any "server": (assuming envs.dbxml is my Xml db)

collection('envs.dbxml')/master/env[@name=$name]/server/domain/application/@*/string()

I want to be able to get the individual server names when this query returns results. How can that be achieved? This query will just return all the applications and status values one after other.


Solution

  • Use:

    (
      /master/env[@name=$name]/server/@name
     |
      /master/env[@name=$name]/server/domain/application/@*
    )
      /string(.)
    

    Or, this shorter form:

    /master/env[@name=$name]/server/(@name|domain/application/@*)/string(.)
    

    Explanation: Here we use the XPath |(union) operator and the XPath 2.0 syntax that allows expressions of the kind: expr/(expr) and expressions of the kind expr/func(argList)