I’m trying to count the occurrences of every value for a specific attribute (e.g. @root) of every element that matches a query. Some of these matching elements do not have the attribute, but I want to count those as well (i.e. also list the amount of matched elements that did not have the attribute)
Currently I use this, but this does not match the nodes that dont have a root attribute. The counting is done by the application, which allows me to show intermediate results.
//node[@rel='su']/@root
I want something like
//node[@rel='su']/string(if .[@root] then @root else 'fallback-value')
The queries are run against Berkeley DB XML so XPath2 and XQuery might also be used to solve this.
edit: To clarify, I am looking for a query which treats the non-existence of the @root attribute as a special case; i.e. as if the attribute did exist and had ‘fallback-value’ as value.
Try this:
Follows the original answer to the misunderstood question
Why don’t you just use
then? It will match both
nodeelements that have a@rootattribute and those who do not.Given that a
nodecan have at most one@rootattributes, counting the nodes is enough.