Reputation: 165
I am trying to use a Xquery FLOWR where statement in C#:
for $x in //div[@class="class1"] where $x//span="data1" return $x
to get some data from an xml file. (The reason for using FLOWR is to easily read the queries runtime from an external file)
for this I use the Saxon API: http://www.saxonica.com/html/documentation/dotnet/dotnetapi.html
The xml code looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<div class="class1">
<div class="class2">
<span class="class3">
data1
</span>
<span class="class4">
data2
</span>
</div>
<div class="class5">
data3
</div>
<div class="class6">
data4
</div>
</div>
<div class="class1">
<div class="class2">
<span class="class3">
data5
</span>
</div>
<div class="class5">
data6
</div>
</div>
</doc>
If I test this code using X-base it works like I expect and returns one of the two class1 classes; however both Saxon and xpathtester.com compile the Xquery without errors but return empty. If I remove the optional where statement it behaves as expected. Am I missing something?
Upvotes: 1
Views: 319
Reputation: 4241
The cause of this is BaseX's CHOP
option, which trims all whitespace at the ends of text nodes. When imported into BaseX with CHOP
enabled (which is the default), you document looks as follows:
<doc>
<div class="class1">
<div class="class2">
<span class="class3">data1</span>
<span class="class4">data2</span>
</div>
<div class="class5">data3</div>
<div class="class6">data4</div>
</div>
<div class="class1">
<div class="class2">
<span class="class3">data5</span>
</div>
<div class="class5">data6</div>
</div>
</doc>
Since the =
operator is whitespace sensitive on strings, your where
clause matches the text contents of the chopped document, but not the original one.
You can trim the whitespace inside your query instead, which works in all cases:
for $x in //div[@class = "class1"]
where $x//span/normalize-space() = "data1"
return $x
Upvotes: 1