Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XQuery to Filter Elements

Reply
Thread Tools

XQuery to Filter Elements

 
 
Zach Brown
Guest
Posts: n/a
 
      08-15-2007
I'm totally stuck on this... I've got an xml fragment that looks like:

<profile id="1">
<meta>
<groupID>100</groupID>
</meta>
<data>
<field id="businessStage" date="8/15/2007">Shipping Product</field>
<field id="businessStage" date="8/14/2007">Development</field>
<field id="businessStage" date="8/13/2007">Research</field>
<field id="ticker" date="8/15/2007">MyTICK</field>
<field id="ticker" date="5/15/2005>TICK</field
</data>
</profile>

And, i was hoping that i could write an xquery expression that would select
basically the whole document minus any fields whose date didn't match
"8/15/2007" resulting in this:

<profile id="1">
<meta>
<groupID>100</groupID>
</meta>
<data>
<field id="businessStage" date="8/15/2007">Shipping Product</field>
<field id="ticker" date="8/15/2007">MyTICK</field>
</data>
</profile>

Basically where I'm at now i either get the whole document returned or just
the fields. Clearly I'm just starting out with experimenting with XQuery
but i'm totally stuck trying to do what I thought was going to be an easy
task.

Any help or thoughts on this would be super appreciated.

Regards,
Zach

 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      08-16-2007

Zach Brown <> wrote in
<D8Mwi.61895$fJ5.36078@pd7urf1no>:
> <profile id="1">
> <meta>
> <groupID>100</groupID>
> </meta>
> <data>
> <field id="businessStage" date="8/15/2007">Shipping
> Product</field> <field id="businessStage"
> date="8/14/2007">Development</field> <field
> id="businessStage" date="8/13/2007">Research</field>
> <field id="ticker" date="8/15/2007">MyTICK</field>
> <field id="ticker" date="5/15/2005>TICK</field
> </data>
> </profile>
>
> And, i was hoping that i could write an xquery expression
> that would select basically the whole document minus any
> fields whose date didn't match "8/15/2007"


So what's the problem? Process all the nodes recursively,
omitting the ones that fit your criteria:

declare function local:filter($node as node())
{
if (local:check($node))
then
if ($node[self::element()])
then
element { name($node) } { local:recurse($node) }
else
if ($node[self::document-node()])
then document { local:recurse($node) }
else $node
else ()
} ;

declare function local:recurse($node as node())
{
for $child in ($node/@*|$node/node())
return local:filter($child)
} ;

declare function local:check($node as node())
{
if ($node[self::field and @date!='8/15/2007'])
then false() else true()
} ;

let $a := fn:doc('filter.xml')
return local:filter($a)

--
"Patience is a minor form of despair, disguised as
virtue." -- Ambrose Bierce
 
Reply With Quote
 
 
 
 
Zach Brown
Guest
Posts: n/a
 
      08-16-2007
Wow. I've only been working around with xquery for the last week. For some
reason, all the stuff that i've managed to find not once mentioned user
defined functions. Powerful.

Thanks for this Pavel. I appreciate it. Sometimes the biggest challenge
when looking for an answer in a new technology is understanding what to
ask... Thanks again.

Regards,
Zach

"Pavel Lepin" <> wrote in message
news:fa0to6$mkb$...
>
> Zach Brown <> wrote in
> <D8Mwi.61895$fJ5.36078@pd7urf1no>:
>> <profile id="1">
>> <meta>
>> <groupID>100</groupID>
>> </meta>
>> <data>
>> <field id="businessStage" date="8/15/2007">Shipping
>> Product</field> <field id="businessStage"
>> date="8/14/2007">Development</field> <field
>> id="businessStage" date="8/13/2007">Research</field>
>> <field id="ticker" date="8/15/2007">MyTICK</field>
>> <field id="ticker" date="5/15/2005>TICK</field
>> </data>
>> </profile>
>>
>> And, i was hoping that i could write an xquery expression
>> that would select basically the whole document minus any
>> fields whose date didn't match "8/15/2007"

>
> So what's the problem? Process all the nodes recursively,
> omitting the ones that fit your criteria:
>
> declare function local:filter($node as node())
> {
> if (local:check($node))
> then
> if ($node[self::element()])
> then
> element { name($node) } { local:recurse($node) }
> else
> if ($node[self::document-node()])
> then document { local:recurse($node) }
> else $node
> else ()
> } ;
>
> declare function local:recurse($node as node())
> {
> for $child in ($node/@*|$node/node())
> return local:filter($child)
> } ;
>
> declare function local:check($node as node())
> {
> if ($node[self::field and @date!='8/15/2007'])
> then false() else true()
> } ;
>
> let $a := fn:doc('filter.xml')
> return local:filter($a)
>
> --
> "Patience is a minor form of despair, disguised as
> virtue." -- Ambrose Bierce


 
Reply With Quote
 
Pavel Lepin
Guest
Posts: n/a
 
      08-17-2007

Please don't top post.

Zach Brown <> wrote in
<pI0xi.63926$fJ5.31293@pd7urf1no>:
> "Pavel Lepin" <> wrote in message
> news:fa0to6$mkb$...
>> Zach Brown <> wrote in
>> <D8Mwi.61895$fJ5.36078@pd7urf1no>:
>>> <profile id="1">
>>> <meta>
>>> <groupID>100</groupID>
>>> </meta>
>>> <data>
>>> <field id="businessStage" date="8/15/2007">Shipping
>>> Product</field> <field id="businessStage"
>>> date="8/14/2007">Development</field> <field
>>> id="businessStage" date="8/13/2007">Research</field>
>>> <field id="ticker" date="8/15/2007">MyTICK</field>
>>> <field id="ticker" date="5/15/2005>TICK</field
>>> </data>
>>> </profile>
>>>
>>> And, i was hoping that i could write an xquery
>>> expression that would select basically the whole
>>> document minus any fields whose date didn't match
>>> "8/15/2007"

>>
>> So what's the problem? Process all the nodes recursively,
>> omitting the ones that fit your criteria:


[snip]

> I've only been working around with xquery for the last
> week.


It's the second XQuery I've ever written, so 'only last
week' sounds like an awful lot of time to do some reading
to me.

> For some reason, all the stuff that i've managed to find
> not once mentioned user defined functions. Powerful.


You've been reading the wrong stuff then. The XQuery spec is
reasonably good as a reference, and definitely worth
skimming over. If that's too hard for you, I'm fairly
certain IBM's developerWorks has some good introductory
reading in its XQuery library:

http://www.ibm.com/developerworks/xml

--
"Patience is a minor form of despair, disguised as
virtue." -- Ambrose Bierce
 
Reply With Quote
 
Zach Brown
Guest
Posts: n/a
 
      08-18-2007
> Please don't top post.

I top posted because my comment was no longer relevant to the initial
question and was just a reflection. Don't make people scroll all the way to
the bottom of a post if the data in the post in no longer relevant to the
current threads voice. Just like the body was removed to clean up the
document because it's no longer relevant to "this" discussion.

Regards,
Zach Brown

 
Reply With Quote
 
Zach Brown
Guest
Posts: n/a
 
      08-18-2007

"Pavel Lepin" <> wrote in message
news:fa0to6$mkb$...
>
> Zach Brown <> wrote in
> <D8Mwi.61895$fJ5.36078@pd7urf1no>:
>> <profile id="1">
>> <meta>
>> <groupID>100</groupID>
>> </meta>
>> <data>
>> <field id="businessStage" date="8/15/2007">Shipping
>> Product</field> <field id="businessStage"
>> date="8/14/2007">Development</field> <field
>> id="businessStage" date="8/13/2007">Research</field>
>> <field id="ticker" date="8/15/2007">MyTICK</field>
>> <field id="ticker" date="5/15/2005>TICK</field
>> </data>
>> </profile>
>>
>> And, i was hoping that i could write an xquery expression
>> that would select basically the whole document minus any
>> fields whose date didn't match "8/15/2007"

>
> So what's the problem? Process all the nodes recursively,
> omitting the ones that fit your criteria:
>
> declare function local:filter($node as node())
> {
> if (local:check($node))
> then
> if ($node[self::element()])
> then
> element { name($node) } { local:recurse($node) }
> else
> if ($node[self::document-node()])
> then document { local:recurse($node) }
> else $node
> else ()
> } ;
>
> declare function local:recurse($node as node())
> {
> for $child in ($node/@*|$node/node())
> return local:filter($child)
> } ;
>
> declare function local:check($node as node())
> {
> if ($node[self::field and @date!='8/15/2007'])
> then false() else true()
> } ;
>
> let $a := fn:doc('filter.xml')
> return local:filter($a)
>
> --
> "Patience is a minor form of despair, disguised as
> virtue." -- Ambrose Bierce


Got this implemented and this worked great BTW. Thanks again.

Best,
Zach

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xquery on elements with attribute named 'type' kishjeff XML 2 12-17-2008 07:52 PM
XQuery how to keep order of elements? paul.rusu@gmail.com XML 7 12-14-2005 09:22 AM
Xquery, repeating elements, Sleepycat's DB XML Bob XML 0 11-16-2005 05:37 AM
xquery question.. how 2 if possible get tuples that have any attributes or elements of a given value Jeff Kish XML 11 10-21-2004 01:56 AM
UV Protector filter vs. Skylight filter? john Digital Photography 8 06-26-2004 03:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57