XQuery with if condition in for loop - xquery

I have written xquery to return results in normal way.
let $results := //data:data
return
<result>
{
for $i in $results
return
<documentInformation>
<id>{data($i/DATA:ID)}</id>
<status>{data($i/#status)}</status>
<title>{data($i/data:title)}</title>
<displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
</documentInformation>
}
</result>
Now, I have to filter out the results in for loop with some condition like
(pseudo logic)
if id = 'abc' and status ="closed"
then skip the row
else add row.
I have tried several ways. but could not run the query..

Try this:
<result>
{
for $i in //data:data
where fn:not($i/DATA:ID = 'abc' and $i/#status = "closed")
return
<documentInformation>
<id>{data($i/DATA:ID)}</id>
<status>{data($i/#status)}</status>
<title>{data($i/data:title)}</title>
<displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
</documentInformation>
}
</result>
Note that the XPath //data:data may have a lot of work to do, but that's a separate matter.

You Can also use if condition instead of where
<result>
{
for $i in //data:data
return
if($i/DATA:ID != 'abc' and $i/#status != "closed")
then
(
<documentInformation>
<id>{data($i/DATA:ID)}</id>
<status>{data($i/#status)}</status>
<title>{data($i/data:title)}</title>
<displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
</documentInformation>
)
else ()
}
</result>

Related

XQuery - Enclosing sequence of elements under a single node

I'm using xquery with BaseX to attempt to retrieve information from JMeter test cases (.jmx files) in a format I desire.
This is the code I'm actually running (inside BaseX GUI):
let $rlist := db:list("JMeter")
for $resource in $rlist
let $rcontent := db:open("JMeter", $resource)
let $ret :=
<HttpRequest>
{
for $item in $rcontent/jmeterTestPlan//HTTPSamplerProxy
return
(<method>
{
$item/stringProp[attribute()/string() = "HTTPSampler.method"]/text()
}
</method>,
<path>
{
let $parse := $item/stringProp[attribute()/string() = "HTTPSampler.path"]/text()
let $parse := fn:replace($parse, "\?[^/]*", "")
let $parse := fn:replace($parse, "\$[^/]*", "\${}")
let $parse := fn:replace($parse, "[0-9]+", "\${}")
return $parse
}
</path>,
<resource>
{
$resource
}
</resource>)
}
</HttpRequest>
return
<HttpRequests>
{
$ret
}
</HttpRequests>
The resulting xml looks like this:
<HttpRequests>
<HttpRequest>
<method>POST</method>
<path>/config</path>
<resource>CentralConfiguration/Requests/addConfiguration.jmx</resource>
</HttpRequest>
</HttpRequests>
<HttpRequests>
<HttpRequest>
<method>POST</method>
<path>/propertyType</path>
<resource>CentralConfiguration/Requests/addPropertyType.jmx</resource>
</HttpRequest>
</HttpRequests>
...
This is what I expect the result to look like:
<HttpRequests>
<HttpRequest>
<method>POST</method>
<path>/config</path>
<resource>CentralConfiguration/Requests/addConfiguration.jmx</resource>
</HttpRequest>
<HttpRequest>
<method>POST</method>
<path>/propertyType</path>
<resource>CentralConfiguration/Requests/addPropertyType.jmx</resource>
</HttpRequest>
...
</HttpRequests>
I'd much appreciate help in formatting my xquery to get the expected result, thanks
The problem was solved by placing the outside for loop inside the first label.

wrapping child node content in xquery

I have what I hope is a really simple question, but I'm a novice at xquery and I can't make this work:
I have the following bit of xml:
<collation>1<num>12</num> 2<num>12</num> ||
I<num>8</num>-V<num>8</num>, 1 flyleaf</collation>
That I need to transform so that becomes the content of a new node, like so:
<note displayLabel="Collation: Notes">1(12) 2(12) || I(8)-V(8), 1 flyleaf<note>
I am using the following xquery code to attempt to do this:
<note displayLabel="Collation:Notes">{for $t in doc("collation.xml")//collation,
$h in distinct-values($t)
return
????
</note>
The problem is that I can either display all of the content (so without the parentheses) using data($t), or I can display just the things I want to be in parentheses (the information in the tags) using data($t/num) but I can't figure out how to display both with the items in tags wrapped in parentheses. I'm sure it's a really simple answer but I can't find it.
This is a good job for recursion:
declare function local:render(
$n as node()
) as node()?
{
typeswitch($n)
case element(num) return text{concat('(', $n, ')')}
case element(collation) return
<note displayLabel="Collation: Notes">{
for $n in $n/node()
return local:render($n)
}</note>
case element() return element { node-name($n) } {
for $n in $n/node()
return local:render($n)
}
default return $n
};
local:render(
<collation>1<num>12</num> 2<num>12</num> || I<num>8</num>-V<num>8</num>, 1 flyleaf</collation>)
=>
<note displayLabel="Collation: Notes">1(12) 2(12) || I(8)-V(8), 1 flyleaf</note>

How to convert text inside a node into child node using xquery update?

I have a xml document like
<root>
<first>
First Level
<second>
second level
<third>
Third Level
</third>
</second>
<second2>
another second level
</second2>
</first>
</root>
How to convert this document with all nodes, that means if a node contains text and child node convert text into a child node (let's say childtext) using xquery-update
<root>
<first>
<childtext>First Level</childtext>
<second>
<childtext>second level</childtext>
<third>
Third Level
</third>
</second>
<second2>
another second level
</second2>
</first>
</root>
And here is what I tried:
let $a :=
<root>
<first>
First Level
<second>
second level
<third>
Third Level
</third>
</second>
<second2>
another second level
</second2>
</first>
</root>
return
copy $i := $a
modify (
for $x in $i/descendant-or-self::*
return (
if($x/text() and exists($x/*)) then (
insert node <childtext>
{$x/text()}
</childtext> as first into $x
(: here should be some code to delete the text only:)
) else ()
)
)
return $i
I could not delete the text which has sibling node.
As you want to replace an element, you should simply use the replace construct, instead of inserting the new element and deleting the old one. Seems much simpler to me:
copy $i := $a
modify (
for $x in $i/descendant-or-self::*[exists(*)]/text()
return replace node $x with <childtext>{$x}</childtext>
)
return $i
The modified solution is from #dirkk here:
copy $i := $a
modify (
for $x in $i/descendant-or-self::*
return (
if($x/text() and exists($x/*)) then (
if(count($x/text())=1) then (
replace node $x/text() with <child> {$x/text()}</child>
) else (
for $j at $pos in 1 to count($x/text())
return
replace node $x/text()[$pos] with <child>{$x/text()[$pos]}</child>
)
) else ()
)
)
return $i
Thank you Once again.

distinct-values() in return result does not work?

I am a newbie in XQuery, and My problem is about distinct values, I am using the following codes to retrieving movie reviewers
xquery version "1.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
$movie in doc("movies.xml")/movies/movie
where $reviews/movie_title = $movie/movie_title
and $movie//movie_genre = "Drama"
and $movie//month > 6
order by $reviews/movie_reviewer descending
return
(<reviewer>{distinct-values($reviews/movie_reviewer)}</reviewer>, '
')
}
</result>
and later I change the code to
xquery version "1.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
$movie in doc("movies.xml")/movies/movie
where $reviews/movie_title = $movie/movie_title
and $movie//movie_genre = "Drama"
and $movie//month > 6
return
{for $content in distinct-values($reviews/movie_reviewer)
order by $content descending
return (<reviewer>{$content}</reviewer>, '
')}
}
</result>
but I got the similar result as
<result>
<reviewer>Wesley Barry</reviewer>
<reviewer>Michael Gordon</reviewer>
<reviewer>Michael Gordon</reviewer>
<reviewer>Michael Gordon</reviewer>
<reviewer>John Frankenheimer</reviewer>
<reviewer>J. Lee Thompson</reviewer>
<reviewer>J. Lee Thompson</reviewer>
<reviewer>Charles Walters</reviewer>
<reviewer>Charles Walters</reviewer>
</result>
how can I make the result like
<result>
<reviewer>Wesley Barry</reviewer>
<reviewer>Michael Gordon</reviewer>
<reviewer>John Frankenheimer</reviewer>
<reviewer>J. Lee Thompson</reviewer>
<reviewer>Charles Walters</reviewer>
</result>
?
I know this is quite basic, but I just can't get the point
Please follow #Ranon's advice. However, guessing from your query: The easiest way would be if you could use XQuery 3.0 and the new group-by-statement.
xquery version "3.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
$movie in doc("movies.xml")/movies/movie
let $reviewer := $reviews/movie_reviewer
where $reviews/movie_title = $movie/movie_title
and $movie//movie_genre = "Drama"
and $movie//month > 6
group by $reviewer
order by $reviewer descending
return
(<reviewer>{$reviewer}</reviewer>, '
')
}
</result>

XQuery: how to properly append , in for loop

So I have xml data like this:
<PhoneNumber>213-512-7457</PhoneNumber>
<PhoneNumber>213-512-7465</PhoneNumber>
and with this XQuery:
<PhoneNumberList>
{
for $phone in $c//PhoneNumber
let $phoneStr := ""
return concat($phoneStr, $phone)
}
</PhoneNumberList>
I get:
<PhoneNumberList>213-512-7457213-512-7465</PhoneNumberList>
But I actually want:
<PhoneNumberList>213-512-7457, 213-512-7465</PhoneNumberList>
Could someone shed some light on how to do this?
<PhoneNumberList>
{
string-join($c//PhoneNumber, ", ")
}
</PhoneNumberList>
There seems to be a lot of confusion with variables in XQuery. A let expression creates a new variable each time it is evaluated, so the "procedural" approaches below will not work.
Whilst the string-join solution is the best in your case, the correct way to write this "manually" is with a recursive function:
declare function local:join-numbers($numbers)
{
concat($numbers[1], ", ", local:join-numbers(substring($numbers,2)))
};
<PhoneNumberList>
{
local:joinNumbers($c//PhoneNumber)
}
</PhoneNumberList>
Ok, something like this should return the first element as-is and the rest with prepended ", "
<PhoneNumberList>
{
for $phone in $c//PhoneNumber[0]
return $phone
for $phone in $c//PhoneNumber[position()>0]
return concat(", ", $phone)
}
</PhoneNumberList>
What about this?
let $phoneStr := ""
<PhoneNumberList>
{
for $phone in $c//PhoneNumber
let $result = concat($phoneStr, $phone)
let $phoneStr = ", "
return $result
}
</PhoneNumberList>

Resources