The (BaseX) error
I am running queries on a large dataset in BaseX, but one XQuery is crashing my programme with an error [XPTY0004] Item expected, sequence found: (attribute begin {"6"}, ...).
.
In my query I am trying to make sure that one element comes before another by comparing begin
- an attribute that is present in XML - with number()
. But whenever I try the most basic of XQueries (return matching nodes) on my dataset (e.g. with this online tool) I get an error that resembles the one that I had before:
[Error] SaxonCE.XSLT20Processor 14:08:39.692 SEVERE: XPathException in invokeTransform: A sequence of more than one item is not allowed as the first argument of number() ("6", "10")
So I am guessing there is something going on with the node's siblings, i.e. that there are more than one of these nodes, and that it's unclear which should be compared. Examples follow below.
Why does the order matter?
The XPath is used in a query engine for treebanks: linguistically annotated corpora. In some cases we want nodes to match in order, and sometimes it does not matter. As a simplistic example: sometimes we want to match something as specific as the concerned man where the order article, adjective, noun matters. In other queries it doesn't matter, and we want to match phrases such as the time available as well, where the order of article, adjective, noun can be in any order.
In other words, in the first case the order of the elements should be respected, in the second one it shouldn't. Here is a possible XPath representation of such a construction that holds an article, an adjective, and a noun.
node[@cat="np" and node[@pt="art"] and node[@pt="adj"] and node[@pt="n"]]
By default, XPath does not care about the order of these elements and does a greedy search, i.e. it will also match items such as the time available (art
, n
, adj
). But I want to re-write the above XPath to make sure that the order of the nodes is respected, and so a construction such as the time available (art
, n
, adj
) is not matched by the concerned man (art
, adj
, n
) is.
# Possible representation of *the time available*
<node id="0" begin="1" cat="np">
<node id="1" begin="1" pt="art" text="the" />
<node id="2" begin="2" pt="n" text="time" />
<node id="3" begin="3" pt="adj" text="available" />
</node>
# Possible representation of *the concerned man*
<node id="0" begin="1" cat="np">
<node id="1" begin="1" pt="art" text="the" />
<node id="2" begin="2" pt="adj" text="concerned" />
<node id="3" begin="3" pt="n" text="man" />
</node>
One way to go about is to use a numeric comparison of the begin
attribute that is available in the corpus. It is numerical ascending, so if we want to ensure the order of XPath is intact, we can say that the numeric value of each child node of @cat="np"
should be less than the next by using number()
. But as I showed above, this leads to an error - an error that would not occur in the simple example code that I just showed.
Another example.
<node id="0" begin="2">
<node id="1" begin="2">
<node id="2" begin="2"/>
<node id="3" begin="3"/>
</node>
<node id="4" begin="5">
<node id="5" begin="5"/>
</node>
<node id="6" begin="6"/>
</node>
This XPath should match:
/node/node[number(@begin) < number(../node/@begin)]
But when put through an XQuery processor you'd get the error described above. A sequence of more than one item is not allowed as the first argument of number() ("2", "5", ...)
.
I tried the solution provided by @Michael Kay but the same issue seems to play.
XQuery
for $node in node[every $n in node[position() lt last()] satisfies (number($n/@begin) lt number($n/following-sibling::node/@begin))]
return $node
Data
<node id="0" begin="2">
<node id="1" begin="2">
<node id="2" begin="2"/>
<node id="3" begin="3"/>
</node>
<node id="4" begin="5">
<node id="5" begin="5"/>
</node>
<node id="6" begin="6"/>
</node>
Error
SaxonCE.XSLT20Processor 14:48:49.809 SEVERE: XPathException in invokeTransform: A sequence of more than one item is not allowed as the first argument of number() ("5", "6")
Update April 19th, 2017
I bumped into some unexpected behaviour today, which makes the solution provided by @har07 not sufficient any more. I had wrongly assumed that the not()
clause had only an effect on the nodes in the XPath (and not all the nodes in XML). In other words, when the not()
clause is added to the topmost node of the XPath, all its children in XML will have a fixed, sorted word order. (Now that I read it like this, it seems only normal.) However, what I actually want is that the word order is only set on the nodes specified in XPath, and not possible other nodes in matching XML. Hopefully and example will make this more clear.
Let's say that I want to match the following XPath, a cat="np"
that contains rel="det" pt="vnw" lemma="die"
and at least two times rel="mod" pt="adj"
.
//node[@cat="np" and node[@rel="det" and @pt="vnw" and @lemma="die"] and count(node[@rel="mod" and @pt="adj"]) > 1]
but with the added requirement that the order of this XPath is followed, i.e.
//node[
@cat="np" and
not(node[
position() < last()
][number(@begin) > following-sibling::node/number(@begin)]) and
node[
@rel="det" and
@pt="vnw" and
@lemma="die"
] and
count(node[
@rel="mod" and
@pt="adj"
]) > 1
]
So rel="det"
has to occur before the two rel="mod"
s in XML. This works fine, and all matches are correct, but not all expected matches are found. The cause is that the not()
line obviously targets all the XML nodes rather than the XPath-specified nodes. In case where down the line a node is found that does not adhere to the not
rule, there won't be a match - even if that node is not specified in XPath. The above XPath, for instance, will not match the following XML because inside cat="np"
there is a node whose begin attribute is larger than its next sibling, which is not allowed by the not
rule.
<node begin="4" cat="np" id="8" rel="obj1">
<node begin="4" id="9" pos="det" pt="vnw" rel="det" word="die" lemma="die" />
<node begin="5" id="10" pos="adj" pt="adj" rel="mod" word="veelzijdige" />
<node begin="6" id="11" pos="adj" pt="adj" rel="mod" word="getalenteerde" />
<node begin="7" id="12" pos="noun" pt="n" rel="hd" word="figuren" />
<node begin="8" id="31" index="1" rel="obj1" />
<node begin="2" id="32" index="2" rel="obj2" />
</node>
However, I would like this cat="np"
to match, and make the not()
function less aggressive, i.e. only require that nodes specified in XPath (in this example rel="det" pt="vnw" lemma="die"
, and the two rel="mod" pt="adj"
nodes) follow the order requirement where the begin attribute should be smaller than the next item of the XPath structure. Other items inside cat="np"
that have not been specified in XPath are allowed to have an attribute that is larger than its next sibling.
Note that the last item of the XPath structure (which would match id="11"
in the example XML) does not necessarily have to have a begin attribute that is lower than its following node in XML (which is not specified in the XPath).
As before, I am especially interested in how to solve this with a pure XPath option, but XQuery alternatives are also welcome. Preferably as a function that takes an XPath structure as input, and applies the 'word order' to its topmost node and all its descendants. Example code and usage with the XPath shown here as an example is encouraged.