Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery

Reply
Thread Tools

obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery

 
 
Jeff Kish
Guest
Posts: n/a
 
      10-30-2008
Hi.

I've been experimenting more with xquery trying to get back into it.

I am not seeing how to, given some query that returns a set of
elements, how to do the following:

given an operation over a collection of documents,
find all the elements <table> that have an attribute called relation,
and return the files names, and some of the attributes of
the returned element and some of their particular ancestor's
attributes.... something like this;

xxx.xml

segment speed="1"
table1 relation="upper"
table3 relation="upper"

xxy.xml
segment speed="2"
table2 relation="lower"


given this xml file set:


document xxx.xml

<area>
<segment id="segment1" speed="1">
<table id="table1" relation="upper" />
<table id="table2" orientation="side" />
<table id="table3" relation="upper" />
</segment>
</area>




document xxy.xml

<area>
<segment id="segment1" speed="2">
<table id="table1" orientation="bottom" />
<table id="table2" relation="lower" />
<table id="table3" orientation"top" />
</segment>
</area>



 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      10-30-2008
Jeff Kish wrote:

> xxx.xml
>
> segment speed="1"
> table1 relation="upper"
> table3 relation="upper"
>
> xxy.xml
> segment speed="2"
> table2 relation="lower"
>
>
> given this xml file set:
>
>
> document xxx.xml
>
> <area>
> <segment id="segment1" speed="1">
> <table id="table1" relation="upper" />
> <table id="table2" orientation="side" />
> <table id="table3" relation="upper" />
> </segment>
> </area>
>
>
>
>
> document xxy.xml
>
> <area>
> <segment id="segment1" speed="2">
> <table id="table1" orientation="bottom" />
> <table id="table2" relation="lower" />
> <table id="table3" orientation"top" />
> </segment>
> </area>


Here is an example XQuery,

string-join(
for $d in
collection('.?select=test20081030*.xml')[descendant::table[@relation]]
return
(tokenize(base-uri($d), '/')[last()], '',
concat('segment speed="',
$d//table[@relation]/ancestor::segment/@speed, '"'),
for $t in $d//table[@relation]
return concat($t/@id, ' relation="', $t/@relation, '"'), '')
, ' ')

that returns the described result when run with Saxon 9 in a working
directory that contains the input files as test20081030nn.xml
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
kishjeff
Guest
Posts: n/a
 
      10-30-2008
On Oct 30, 8:53*am, Martin Honnen <(E-Mail Removed)> wrote:
> Jeff Kish wrote:
> > xxx.xml

>
> > segment speed="1"
> > table1 relation="upper"
> > table3 relation="upper"

>
> > xxy.xml
> > segment speed="2"
> > table2 relation="lower"

>
> > given this xml file set:

>
> > document xxx.xml

>
> > <area>
> > <segment id="segment1" speed="1">
> > <table id="table1" *relation="upper" * */>
> > <table id="table2" *orientation="side" />
> > <table id="table3" *relation="upper" * />
> > </segment>
> > </area>

>
> > document xxy.xml

>
> > <area>
> > <segment id="segment1" speed="2">
> > <table id="table1" *orientation="bottom" * */>
> > <table id="table2" *relation="lower" />
> > <table id="table3" *orientation"top" * />
> > </segment>
> > </area>

>
> Here is an example XQuery,
>
> string-join(
> for $d in
> collection('.?select=test20081030*.xml')[descendant::table[@relation]]
> return
> * *(tokenize(base-uri($d), '/')[last()], '',
> * *concat('segment speed="',
> $d//table[@relation]/ancestor::segment/@speed, '"'),
> * *for $t in $d//table[@relation]
> * *return concat($t/@id, ' relation="', $t/@relation, '"'), '')
> , ' ')
>
> that returns the described result when run with Saxon 9 in a working
> directory that contains the input files as test20081030nn.xml
> --
>
> * * * * Martin Honnen
> * * * *http://JavaScript.FAQTs.com/


Thanks. I'm using IPSI XQuery Interpreter v.1.3.4 which may be a bit
long in the tooth (I don't think it is complete as I could not get
starts-with to work) - let me give this a fly.

thanks much Martin.

Jeff
 
Reply With Quote
 
kishjeff
Guest
Posts: n/a
 
      10-30-2008
On Oct 30, 11:29*am, kishjeff <(E-Mail Removed)> wrote:
> On Oct 30, 8:53*am, Martin Honnen <(E-Mail Removed)> wrote:
>
>
>
> > Jeff Kish wrote:
> > > xxx.xml

>
> > > segment speed="1"
> > > table1 relation="upper"
> > > table3 relation="upper"

>
> > > xxy.xml
> > > segment speed="2"
> > > table2 relation="lower"

>
> > > given this xml file set:

>
> > > document xxx.xml

>
> > > <area>
> > > <segment id="segment1" speed="1">
> > > <table id="table1" *relation="upper" * */>
> > > <table id="table2" *orientation="side" />
> > > <table id="table3" *relation="upper" * />
> > > </segment>
> > > </area>

>
> > > document xxy.xml

>
> > > <area>
> > > <segment id="segment1" speed="2">
> > > <table id="table1" *orientation="bottom" * */>
> > > <table id="table2" *relation="lower" />
> > > <table id="table3" *orientation"top" * />
> > > </segment>
> > > </area>

>
> > Here is an example XQuery,

>
> > string-join(
> > for $d in
> > collection('.?select=test20081030*.xml')[descendant::table[@relation]]
> > return
> > * *(tokenize(base-uri($d), '/')[last()], '',
> > * *concat('segment speed="',
> > $d//table[@relation]/ancestor::segment/@speed, '"'),
> > * *for $t in $d//table[@relation]
> > * *return concat($t/@id, ' relation="', $t/@relation, '"'), '')
> > , ' ')

>
> > that returns the described result when run with Saxon 9 in a working
> > directory that contains the input files as test20081030nn.xml
> > --

>
> > * * * * Martin Honnen
> > * * * *http://JavaScript.FAQTs.com/

>
> Thanks. I'm using IPSI XQuery Interpreter v.1.3.4 which may be a bit
> long in the tooth (I don't think it is complete as I could not get
> starts-with to work) - let me give this a fly.
>
> thanks much Martin.
>
> Jeff


This was soooo helpful, thanks.
I have a related question..

I tried using this for fun (notice the extra base-uri($d) in the
beginning of the last
return statement):

string-join(
for $d in
collection('.?select=test20081030*.xml')[descendant::table[@relation]]
return
base-uri($d),(tokenize(base-uri($d), '/')[last()], '',
concat('segment speed="',
$d//table[@relation]/ancestor::segment/@speed, '"'),
for $t in $d//table[@relation]
return concat($t/@id, ' relation="', $t/@relation, '"'), '')
, ' ')

I noticed that when I use this instead, I get the error:

XPST0008: XQuery static error in #...ri($d),(tokenize(base-
uri($d),#:
Variable $d has not been declared
Static error(s) in query


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-30-2008
kishjeff wrote:

> I tried using this for fun (notice the extra base-uri($d) in the
> beginning of the last
> return statement):
>
> string-join(
> for $d in
> collection('.?select=test20081030*.xml')[descendant::table[@relation]]
> return


Add parentheses e.g
(base-uri($d),(tokenize(base-uri($d), '/')[last()], '',
> base-uri($d),(tokenize(base-uri($d), '/')[last()], '',
> concat('segment speed="',
> $d//table[@relation]/ancestor::segment/@speed, '"'),
> for $t in $d//table[@relation]
> return concat($t/@id, ' relation="', $t/@relation, '"'), '')

)
> , ' ')




--

Martin Honnen
http://JavaScript.FAQTs.com/
 
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
[XSLT] Select node by attribute with value from an attribute of its self Stefan_W XML 1 11-05-2008 10:50 AM
Get an element's ancestor node based on it's id? harryajh Javascript 4 02-01-2008 12:06 AM
Query : Number of Attributes under an element & atleast 1 occurence of element/Attribute kosaraju.puneeth@gmail.com XML 0 10-26-2005 07:09 AM
How to get values of hidden elemnts? Omar Javascript 1 04-24-2005 11:39 AM
How to output the all the ancestor and their attribute of an element ai2003lian@yahoo.com XML 2 02-03-2005 06:03 PM



Advertisments