eXist: using // on a document fragment
Posted by jamie on 12 May 2011 in Notes
Be careful using the "//" axis to iterate over a set of elements in a document fragment, since it doesn't work as you may expect. Take this XML, which I'll call $summary:
<p>
<span class="previous">... sens français, </span>
<span class="hi">francophone</span>
<span class="following"> également, mais quelquefois je me s ...</span>
</p>
<p>
<span class="previous">... ment, mais quelquefois je me sens plus </span>
<span class="hi">francophone</span>
<span class="following"> que français ...</span>
</p>
<p>
<span class="previous">... je ... je crois que notre culture </span>
<span class="hi">francophone</span>
<span class="following"> hein .. ...</span>
</p>
To loop through the "p" elements, you cannot use $summary//p since this is a document fragment. The <p> elements do not have a root element to which they belong, thus $summary//p has no proper context to find the 'child' <p> elements in $summary. Instead, use either the descendant-or-self or the self axis to loop through elements in such a fragment:
for $item in $summary/descendant-or-self::p
...