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

Discussion in 'XML' started by Jeff Kish, Oct 30, 2008.

  1. Jeff Kish

    Jeff Kish Guest

    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>
     
    Jeff Kish, Oct 30, 2008
    #1
    1. Advertising

  2. Re: obtain element name, or attribute and value of the document nameitself, and some elemnts and attributes from an ancestor or the node itselfusing xquery

    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/
     
    Martin Honnen, Oct 30, 2008
    #2
    1. Advertising

  3. Jeff Kish

    kishjeff Guest

    Re: obtain element name, or attribute and value of the document nameitself, and some elemnts and attributes from an ancestor or the node itselfusing xquery

    On Oct 30, 8:53 am, Martin Honnen <> 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
     
    kishjeff, Oct 30, 2008
    #3
  4. Jeff Kish

    kishjeff Guest

    Re: obtain element name, or attribute and value of the document nameitself, and some elemnts and attributes from an ancestor or the node itselfusing xquery

    On Oct 30, 11:29 am, kishjeff <> wrote:
    > On Oct 30, 8:53 am, Martin Honnen <> 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
     
    kishjeff, Oct 30, 2008
    #4
  5. Re: obtain element name, or attribute and value of the document nameitself, and some elemnts and attributes from an ancestor or the node itselfusing xquery

    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/
     
    Martin Honnen, Oct 30, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthet
    Replies:
    2
    Views:
    711
    Matthet
    Sep 24, 2003
  2. Chris Bedford
    Replies:
    1
    Views:
    692
    Ben Edgington
    May 21, 2004
  3. Omar
    Replies:
    1
    Views:
    96
    Thomas 'PointedEars' Lahn
    Apr 24, 2005
  4. Stuart McDougall

    can JS pick up LANG attrib's ..?

    Stuart McDougall, May 29, 2006, in forum: Javascript
    Replies:
    5
    Views:
    134
    Thomas 'PointedEars' Lahn
    May 30, 2006
  5. Terry
    Replies:
    11
    Views:
    190
    Dr J R Stockton
    Sep 4, 2007
Loading...

Share This Page