user9517536248
user9517536248

Reputation: 355

Else if ladder in Xquery

I have an xml file as:

<search>
    <parameter name=”Title” included=”true”>I Want to Be Somebody New!</parameter>
    <parameter name=”Author” included=”true”>Lopsh</parameter>
    <parameter name=”ISBN” included=”false”/>
</search>

What i want to do is if included is true and name is Title get the data of title. I searched everywhere for else if ladder but can't seem to find one. I tried this code

let $params := doc('params.xml')

for $param in $params
    let $title := if ($param/@name="Title" and $param/@included="true") then $param/data(.) else ' '
    let $author := if ($param/@name="Author" and $param//@included="true") then $param/data(.) else ' '
    let $isbn := if ($param/@name="ISBN" and $param//@included="true") then $param/data(.) else ' '

return $title

Its still not working

Upvotes: 0

Views: 139

Answers (2)

adamretter
adamretter

Reputation: 3517

For what you are requesting "included is true and name is Title get the data of title", you can just use a very simple XPath:

data(/search/parameter[@included eq "true"][@name eq "Title"])

But perhaps your question is not easily understood, as your attempt at the code seems much more complicated than that?!?

Upvotes: 1

wst
wst

Reputation: 11771

You are not iterating over <parameter> elements. doc(...) returns a document-node, and its first child is the root element, in this case <search>. To iterate over the values you want, add XPath to select them:

for $param in $params/search/parameter

Upvotes: 1

Related Questions