2

I am reading about and testing XQuery and like test tools I use BaseX(www.basex.org) and saxon-HE 9.4.0.6N. For the following simple XML file - no schema attached to the sample.xml:

    <rootab>
     <l1>
       <items p="a">
         <itema x1="10" id="abc">testa</itema>
         <itemb x1="10" id="dfe">testb</itemb>
         <itemc x1="10" id="jgh">testc</itemc>  
       </items>
     </l1> 
     <l2>
         <items p="b">
           <itema x1="10" xidref="abc">testa</itema>
           <itemc x1="10" xidref="jgh">testc</itemc>  
           <itemd x1="10" xidref="abc">testA101</itemd>  
           <iteme x1="10" xidref="jgh">testB202</iteme>  
         </items>
     </l2> 
</rootab>

In Basex_GUI if I enter the following XPath expression: //idref("abc")/.. the result is: <itema x1="10" xidref="abc">testa</itema>

In BaseX_GUI if I add the simple XQuery expression:

for $x in doc('sample.xml')//idref("abc")/.. 
    return <aaa>{$x}</aaa>

the result is:

<aaa>
  <itema x1="10" xidref="abc">testa</itema>
</aaa>
<aaa>
  <itemd x1="10" xidref="abc">testA101</itemd>
 </aaa>

q1) Why the XPath expression returned only one node? I expected two...

In Saxon, by using the below xql file:

    <test>
    {
    doc('sample.xml')//idref("abc")/..
    }
   </test>

or the XQuery expression , I receive the same result by running the command query sample.xql:

<?xml version="1.0" encoding="UTF-8"?><test/>

q2)what is wrong in my Saxon test ?

thank you in advance for your help!

dag
  • 288
  • 2
  • 10

1 Answers1

3

Basically, idref() is sensitive to DTD validation - it recognizes attributes declared as type IDREF in your DTD.

You haven't shown us your DTD, and more importantly, you haven't shown how the input to the queries is supplied. There are many ways of constructing input in which the "IDREF-ness" of an attribute is lost - for example, going via a DOM. Even when you use the doc() function in Saxon, the way the input tree is built depends on many factors including configuration options and your URIResolver.

I see you are using .NET. When Saxon uses the Microsoft XML parser on .NET, it doesn't know which attributes are IDs and IDREFs, so the id() and idref() functions don't work (the MS parser simply doesn't supply this information). You therefore need to use the JAXP parser (Xerces) that comes with the Saxon product. I think this is the default these days.

So not really an answer, but hopefully some background that explains some of the things that can go wrong.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • there is no DTD...the sample.xml has been written after I read some chapters in your book . – dag Dec 05 '12 at 13:04
  • If there is no DTD and no schema, then the system has no way of knowing which attributes are IDREF attributes, so the idref() function will never return anything. – Michael Kay Dec 05 '12 at 17:34
  • if i write my own DTD, is it possible to use it (internal DTD or external reference) from the command line with Saxon? (e.g. query -useDTD:x sample.xql). But programmatically, in .Net? The DTD file supposes to be pretty simple only what I need - some attributes to be declared like IDREF type. Thank you very much! – dag Dec 05 '12 at 19:12