[xquery-talk] function xquery

G. Ken Holman gkholman at CraneSoftwrights.com
Sun Nov 21 12:00:39 PST 2010

At 2010-11-21 16:50 +0000, Mohamed kharrat wrote:
>Dear all,
>i have this function:
>declare function local:Match($X as xs:string*,$Y as xs:string*)
>let $count:=tokenize($X, "\s")
>every $tok2 in tokenize($Y, "\s") satisfies
>some  $l in $count satisfies
>contains($l,$tok2)(:contains ($l,$tok):)
>return true()
>it works great but i dont want String parametres
>i want use:  doc('v2.xml')//newsItem/itemMeta/title/text() as parametre

I would not work with the value this way because it is too 
fragile.  In my work I very very rarely address text nodes.  You want 
the text of the title, that is a string, and the value of the title 
element is a string of all of the descendent text concatenated together.

Consider this:

    <title>War of the Worlds</title>

... that looks simple enough, but what about the following:

    <title>War of the<!--capitals or not?--> Worlds</title>

That has two text nodes, not one.  If you declared a function 
parameter as being a single node, it would fail because addressing 
all of the text node children of <title> will give you two nodes, not one.

I cringe when I see so many XQuery examples addressing text nodes 
using text() when I seriously doubt that is what the writer really 
wants.  In the classroom I emphasize the use of the element's string 
value rather than dealing with text nodes.  If you think you need to 
address a text node, think again ... you probably don't.

>not a String "my string"
>i changed the function to : declare function local:Match($X as 
>node() ,$Y as node() )
>but it does't work,

You don't say why it doesn't work, so how can we help you?

>i must convert doc('v2.xml')//newsItem/itemMeta/title/text() to 
>string ? or i must change something else?

It is safest to convert doc('v2.xml')//newsItem/itemMeta/title to a 
string, not to convert the text node.

I hope this helps, but you aren't giving volunteers much to work with.

. . . . . . . . . Ken

Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
G. Ken Holman                 mailto:gkholman at CraneSoftwrights.com
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

More information about the talk mailing list