Two of the ways (calling doc() and calling saxon:parse()) return a document node with an alist element as its only child. The third way, "let $intdoc := <alist>..." produces an element node rather than a document node. If you want it to behave the same way as the other two, write
 
let $intdoc := document { <alist>...</alist> }
 
Michael Kay
http://www.saxonica.com/ 


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Cecil New
Sent: 17 December 2007 12:52
To: Mailing list for SAXON XSLT queries
Subject: Re: [saxon] Iterating over a collection

Thanks for responses.  There appears to be some subtleties here that I need to appreciate.  I love to read, so please feel free to point me somewhere if the below is old hat.
 
The below example shows three ways of handling the "same" content.  It builds a new XML document containing results of selecting on "/alist".  Two of the three cases produces what I had expected.  The middle one doesn't.  Thanks for your insights.
 
Here is the xquery:

(:
********************* alist.xml looks like this
<alist>
<ssid id="10"/>
</alist>
:)

let $extdoc := doc("alist.xml")

let $intdoc :=
<alist>
 <ssid id="10"/>
</alist>

let $strdoc := saxon:parse('<alist><ssid id="10"/></alist>')

return
<root>
<extdoc>
{$extdoc/alist}
</extdoc>
<intdoc>
{$intdoc/alist}
</intdoc>
<strdoc>
{$strdoc/alist}
</strdoc>
</root>

 Here is the output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <extdoc>
      <alist>
         <ssid id="10"/>
      </alist>
   </extdoc>
   <intdoc/>
   <strdoc>
      <alist>
         <ssid id="10"/>
      </alist>
   </strdoc>
</root>


 
On 12/15/07, Dimitre Novatchev <dnovatchev@gmail.com> wrote:
First of all, you do not seem to be asking any question at all.

I assume you might be interested in an explanation of the observed behavior.

If so, then the answer is very simple. As defined, the value of $intdoc is:

   <alist>
     <ssid id="258070" size="20"/>
     <ssid id="258070" size="10"/>
  </alist>


and $intdoc has no child named "alist" (it is itself the "alist" element).

Therefore, any XPath expression starting with
   $intdoc/alist

will not select anything.



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play




On Dec 15, 2007 9:47 AM, Cecil New <cecil.new@gmail.com> wrote:
> I have thousands of small XML files that I wanted to get some basic metrics
> on.  I used an intermediate document to do so.  But in the process I found
> something that looks like it may be a bug.  Here are the details using a
> simplified case.
>
> a) each xml file looks like these two:
> <archive ssid="258070" name="eship-20010919" size="20" >
> <annotation>
> anot1
> </annotation>
> </archive>
>
>
> b) This query works ok.  the query is followed by the output
>
>
> let $intdoc :=
> <alist>
> {
> for $doc in
>
> collection("file:///d:/Users/asrdcnt/misc/xquery/Collections/bug?select=f*.xml;on-error=fail
> ")
> let $ssid := $doc/archive/@ssid
> let $size := $doc/archive/@size
> return
>  <ssid id="{$ssid}" size="{$size}" />
> }
> </alist>
>
> return
> <total>
> {$intdoc}
> </total>
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <total>
>    <alist>
>       <ssid id="258070" size="20"/>
>       <ssid id="258070" size="10"/>
>    </alist>
> </total>
> {$intdoc}
> </total>
>
>  c) This query fails (output follows):
>
>
> let $intdoc :=
> <alist>
> {
> for $doc in
>
> collection("file:///d:/Users/asrdcnt/misc/xquery/Collections/bug?select=f*.xml;on-error=fail
> ")
> let $ssid := $doc/archive/@ssid
> let $size := $doc/archive/@size
> return
>  <ssid id="{$ssid}" size="{$size}" />
> }
> </alist>
>
> return
> <total>
> {$intdoc/alist}
> </total>
>  <?xml version="1.0" encoding="UTF-8"?>
> <total/>
> d) This also fails with same empty root <total/>.
>
>
> let $intdoc :=
> <alist>
> {
> for $doc in
>
> collection("file:///d:/Users/asrdcnt/misc/xquery/Collections/bug?select=f*.xml;on-error=fail
> ")
> let $ssid := $doc/archive/@ssid
> let $size := $doc/archive/@size
> return
>  <ssid id="{$ssid}" size="{$size}" />
> }
> </alist>
>
> return
> <total>
> {$intdoc/alist/ssid}
> </total>
>  e) Finally, and surprisingly, this works:
>
>
> let $intdoc :=
> <alist>
> {
> for $doc in
>
> collection("file:///d:/Users/asrdcnt/misc/xquery/Collections/bug?select=f*.xml;on-error=fail
> ")
> let $ssid := $doc/archive/@ssid
> let $size := $doc/archive/@size
> return
>  <ssid id="{$ssid}" size="{$size}" />
> }
> </alist>
>
> return
> <total>
> {$intdoc//ssid}
> </total>
>
>  <?xml version="1.0" encoding="UTF-8"?>
> <total>
>    <ssid id="258070" size="20"/>
>    <ssid id="258070" size="10"/>
> </total>
> f) And finally, summing all sizes works using the version that works
>
>
> let $intdoc :=
> <alist>
> {
> for $doc in
>
> collection("file:///d:/Users/asrdcnt/misc/xquery/Collections/bug?select=f*.xml;on-error=fail
> ")
> let $ssid := $doc/archive/@ssid
> let $size := $doc/archive/@size
> return
>  <ssid id="{$ssid}" size="{$size}" />
> }
> </alist>
>
> return
> <total>
> {sum($intdoc//ssid/@size)}
> </total>
>
>  <?xml version="1.0" encoding="UTF-8"?>
> <total>30</total>
>
> Regards
> -------------------------------------------------------------------------
> SF.Net email is sponsored by:
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services
> for just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
> _______________________________________________
> saxon-help mailing list
> saxon-help@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
saxon-help mailing list
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help