I'm ordering search results (from many XML files) according to the contents of the XML element . Tricky thing is I need to order the results according to only a part of the string found in this element, namely the number following the string 'Rep.'. For instance 'BPH, Rep. 49, T I Nr. 22' should come before 'I. HA Rep. 100, Nr. 2233'. I use regex to isolate the substring with fn:replace. The regex is definitely correct, it spits out the right number every time. For some reason it won't order correctly though. What am I missing? Thanks from a newbie.
The XML looks like this:
<xml>
<idno>I. HA Rep. 100, Nr. 2233</idno>
<idno>I. HA Rep. 100, Nr. 2535</idno>
<idno>BPH, Rep. 113, Nr. 1694</idno>
<idno>BPH, Rep. 113, Nr. 2845</idno>
<idno>BPH, Rep. 192 NL Wittgenstein, IV, 2, 14</idno>
<idno>BPH, Rep. 49, T I Nr. 21</idno>
<idno>BPH, Rep. 49, T I Nr. 22</idno>
<idno>(D) BPH, Rep. 48, Nr. 141</idno>
<idno>(D) BPH, Rep. 48, Nr. 144</idno>
<idno>BPH, Rep. 192, NL Wittgenstein, VI,9,7</idno>
<idno>I. HA Rep. 178 F, Nr. 31</idno>
</xml>
Xquery is like this:
for $record in $records
order by replace($record//idno, '(.*Rep\.\s)(\d+)(.*)', '$2')
return $record//idno
The result:
I. HA Rep. 100, Nr. 2233
I. HA Rep. 100, Nr. 2535
BPH, Rep. 113, Nr. 2845
BPH, Rep. 113, Nr. 1694
I. HA Rep. 178 F, Nr. 31
BPH, Rep. 192, NL Wittgenstein, VI,9,7
BPH, Rep. 192 NL Wittgenstein, IV, 2, 14
(D) BPH, Rep. 48, Nr. 141
(D) BPH, Rep. 48, Nr. 144
BPH, Rep. 49, T I Nr. 21
BPH, Rep. 49, T I Nr. 22
Desired result:
(D) BPH, Rep. 48, Nr. 141
(D) BPH, Rep. 48, Nr. 144
BPH, Rep. 49, T I Nr. 21
BPH, Rep. 49, T I Nr. 22
I. HA Rep. 100, Nr. 2233
I. HA Rep. 100, Nr. 2535
BPH, Rep. 113, Nr. 1694
BPH, Rep. 113, Nr. 2845
I. HA Rep. 178 F, Nr. 31
BPH, Rep. 192, NL Wittgenstein, VI,9,7
BPH, Rep. 192 NL Wittgenstein, IV, 2, 14
If I change my 'order by' to something simple - like, ordering by a different element without using regex - it works. I can't figure out how it is ordering. We are talking about thousands of idnos, and I can't figure out the pattern it is supposedly ordering them by. I can add more to the list though if someone needs more examples to figure it out. Thank you!!!!
I was unable to test using your input and sample XQuery, but try wrapping
replace()
innumber()
...