[xquery-talk] more readable format - can let clause help me here

Jeni Tennison jeni at jenitennison.com
Mon Nov 1 10:32:25 PST 2004


Hi pJ,

> I am using VB.net and microsoft.xml.xquery.dll. I want to break this
> long xquery into more readable format. Can someone please suggest me
> what I am doing wrong.

You are setting the value of the $c1 and $c2 (XQuery) variables to
strings, then referring to those variables and expecting the strings
to be dynamically evaluated as expressions. XQuery can't do dynamic
evaluation of strings as expressions.

An alternative would be to set VB variables to strings, and build up
the query from those strings with something like (forgive my lack of
knowledge of VB syntax):

c1 = "(RP02/TypeCode='NW' and RP05/Type='0')"
c2 = "(RP02/LaborCode='G' and RP02/ModCode='LK' and
       RP02/PartCode!='SL' and RP02/ModCode!='SL' and RP05/Type='0' )"

query = "let $price:=sum((document(""f1"")//Repair[" & c1 & " or " &
c2 & "]/RP01/DisplayPrice))" & _
        "return <Amount>{$price}</Amount>"

If you don't want to do that, the right thing to do in XQuery is to
declare functions that return the results of the two tests. The query
would look something like:

declare function local:c1 ($repair as element(Repair)) as xs:boolean {
  $repair/RP02/TypeCode='NW' and $repair/RP05/Type='0'
};

declare function local:c2 ($repair as element(Repair)) as xs:boolean {
  $repair/RP02/LaborCode='G' and $repair/RP02/ModCode='LK' and
  $repair/RP02/PartCode!='SL' and $repair/RP02/ModCode!='SL' and
  $repair/RP05/Type='0'
};

let $price = sum(document("f1")//Repair[local:c1(.) or local:c2(.)]/RP01/DisplayPrice)
return <Amount>{$price}</Amount>

(untested)

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



More information about the talk mailing list