On 3/8/06, Jochen Rebhan <j.rebhan@web.de> wrote:

Hi ,

I am having some problems reading an external file, the "no" attribute gets incremented by 2 instead of 1.

If I declare the external xml File as interal variable with "let", everyting works as expected, the "no" attribute gets incremented by 1.

Here the snippets:

(: the xquery file :)

xquery version "1.0";

declare variable $input external;

declare function local:copy($number_in as xs:integer, $node as element() , $level as xs:integer) as item()* {
    element { node-name($node ) }
        if ($level eq 1) then attribute no { $number_in } else (),
        for $! c at $number_out in $node/child::node() return
                case $e as element() return local:copy( $number_out, $e, $level + 1)
                case $t as text()    return $t
                default              return <error/>

local:copy(0,$input/*, 0)


(: the input file :)

<?xml version="1.0" encoding="UTF-8"?>
   &nb! sp;    <level1 attr="someatt">some text</level 1>
        <level1>some text value</level1>
        <level1 attr="ter">some text</level1>
        <level1 someattr="something">
                <someelem>simple text</someelem>
                <someelems>simple textm</someelems>
        <level1 someattr="more text">
   !      </level1>
        <level1 someattr="some other text">
                <someelem>simple text1</someelem>
                <someelems>simple text2</someelems>
        <level1 someattr="sometext">
                <someelem attr="someatt">some text</someelem>
                <someelems attr="someatt">some text</someelems>


(: the windows .bat file I use! :)

java  -cp saxon8.jar net.sf.saxon.Query -o test.out.xml test.xq +input=test.in.xml


(: the output looks like that, note the "no" attribute gets incremented by 2 instead of 1 :)

<?xml version="1.0" encoding="UTF-8"?>
        <level1 no="2">something</level1>
        <level1 no="4">some text</level1>
        <level1 no="6">some text value</level1>

Any idea why 2 ? As I said, when I define the Xml Input document as variable inside the Xquery file everything works like expected !

Any help is appreciated, thanks.


I don't know  XQuery sufficiently yet to debug your query, but this sort of thing usually boils down to counting whitespace nodes when you dont really want to.

Try changing:

for $! c at $number_out in $node/child::node() return


for $! c at $number_out in $node/child::* return

and see if that helps (node() includes whitespace only text nodes, * doesn't)