[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Better way to structure XQuery

G. Ken Holman gkholman at CraneSoftwrights.com
Wed Jul 8 13:32:27 PDT 2009


  Better way to structure XQuery
At 2009-07-08 11:08 -0500, http://x-query.com/mailman/listinfo/talk wrote:
>I have the following XQuery:
>
>xquery version="1.0";
>
>let $mydoc :=doc('http://localhost:8080/exist/rest/db/bio.xml')
>let $profileType := 'Standard'
>let $profileType := if
>($mydoc/metadata/idinfo/spdom/bounding/boundalt/altmin or
>$mydoc/metadata/idinfo/spdom/bounding/boundalt/altmax) then 'Biological'
>else $profileType
>
>return
>    <result>
>       {$profileType>
>    </result>

As an aside, I would have written that as:

xquery version="1.0";
<result>
  { if ( doc('http://localhost:8080/exist/rest/db/bio.xml')/
         metadata/idinfo/spdom/bounding/boundalt/altmin or
         doc('http://localhost:8080/exist/rest/db/bio.xml')/
         metadata/idinfo/spdom/bounding/boundalt/altax )
       then 'Biological" else 'Standard' }
</result>

... because I would assume that the processor would have cached the 
document in memory during the transformation rather than accessing 
the resource each time.  I'm unsure why you would have used and 
re-used the $profileType variable.

>This works fine, however, I would like to take the paths and place them
>in a sequence (there are potentially 30+ I need to check for). I am new
>to XQuery/XPath and unable to figure out the proper way to do this.
>
>Can anyone let me know how to use two variables, one being the $mydoc
>and one being a string ($path :=
>'/metadata/idinfo/spdom/bounding/boundalt/altmin') and have it evaluate
>as XPath instead of as a string?

There is no equivalent to eval() available to you.

However, if you have a text serialization available in your XQuery 
implementation then XQuery is composable (XSLT moreso I find because 
it is all simple XML) so you can run two steps, the first to compose 
the necessary XQuery that is run in the second.

I have an example below that uses Saxon.  I have no idea what you 
want to do with your XPath expressions since you are asking about 
getting them one at a time, yet you are using two of them in your sample.

I hope this helps give you an idea.

. . . . . . . . . . . . . Ken

p.s. I hope on Friday to announce a hands-on XQuery class in 
California this summer ... plans are in the works and there is a 
survey out to help determine interest in particular delivery dates

T:\ftemp>type stacy.xml
<xpaths>
<xpath>/metadata/idinfo/spdom/bounding/boundalt/altmin</xpath>
<xpath>/metadata/idinfo/spdom/bounding/boundalt/altmax</xpath>
</xpaths>

T:\ftemp>type stacy.xq
declare namespace saxon = "http://saxon.sf.net/";
declare option saxon:output "method=text";

"let $mydoc := doc( 'bio.xml' )
     return &lt;result>
",
for $each in xpaths/xpath
     return concat("&lt;subresult>
              {if ($mydoc",$each,") then 'Biological' else 'Standard'}
&lt;/subresult>"),
"&lt;/result>"

T:\ftemp>xquery stacy.xml stacy.xq generated.xq

T:\ftemp>type generated.xq
let $mydoc := doc( 'bio.xml' )
     return <result>
  <subresult>
              {if 
($mydoc/metadata/idinfo/spdom/bounding/boundalt/altmin) then 'B
iological' else 'Standard'}
</subresult> <subresult>
              {if 
($mydoc/metadata/idinfo/spdom/bounding/boundalt/altmax) then 'B
iological' else 'Standard'}
</subresult> </result>
T:\ftemp>xquery bio.xml generated.xq
Source document ignored - query does not access the context item
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <subresult>Standard</subresult>
    <subresult>Standard</subresult>
</result>
T:\ftemp>



--
Possible July/August XSLT/XQuery/XSL-FO training in Oakland/CA/USA
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:http://x-query.com/mailman/listinfo/talk
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.