obtain element name, or attribute and value of the document name itself, and some elemnts and attrib

J

Jeff Kish

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>
 
M

Martin Honnen

Jeff said:
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
 
K

kishjeff

Jeff said:
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

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
 
K

kishjeff

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
 
M

Martin Honnen

kishjeff said:
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, '"'), '') )
, '
')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top