xquery question.. how 2 if possible get tuples that have any attributes or elements of a given value

Discussion in 'XML' started by Jeff Kish, Oct 19, 2004.

  1. Jeff Kish

    Jeff Kish Guest

    Just wondering if it was possible to xquery an xml file
    to retrieve all elements that had a given text value
    or all elements where any attribute had a value of interest.

    I guess I could figure out a roughshod way to get the elements but I'm not sure about the attributes



    Thanks
     
    Jeff Kish, Oct 19, 2004
    #1
    1. Advertising

  2. Jeff Kish <> writes:

    > Just wondering if it was possible to xquery an xml file
    > to retrieve all elements that had a given text value


    //*[.='value']

    > or all elements where any attribute had a value of interest.


    //*[@*='value']
    >
    > I guess I could figure out a roughshod way to get the elements but I'm not sure about the attributes
    >
    >
    >
    > Thanks



    David
     
    David Carlisle, Oct 19, 2004
    #2
    1. Advertising

  3. Jeff Kish

    Jeff Kish Guest

    On Tue, 19 Oct 2004 08:55:41 GMT, David Carlisle <> wrote:

    >Jeff Kish <> writes:
    >
    >> Just wondering if it was possible to xquery an xml file
    >> to retrieve all elements that had a given text value

    >
    >//*[.='value']
    >
    >> or all elements where any attribute had a value of interest.

    >
    >//*[@*='value']
    >>
    >> I guess I could figure out a roughshod way to get the elements but I'm not sure about the attributes
    >>
    >>
    >>
    >> Thanks

    >
    >
    >David

    Thank you David.
     
    Jeff Kish, Oct 19, 2004
    #3
  4. Jeff Kish

    Jeff Kish Guest

    On Tue, 19 Oct 2004 08:55:41 GMT, David Carlisle <> wrote:

    >Jeff Kish <> writes:
    >
    >> Just wondering if it was possible to xquery an xml file
    >> to retrieve all elements that had a given text value

    >
    >//*[.='value']
    >
    >> or all elements where any attribute had a value of interest.

    >
    >//*[@*='value']
    >>
    >> I guess I could figure out a roughshod way to get the elements but I'm not sure about the attributes
    >>
    >>
    >>
    >> Thanks

    >
    >
    >David

    Is there a way to access the element type?
    For example, if the where clause filters out unwanted elements,
    can I somehow output just the type of element we found things in, rather than the entire element?

    I would guess in some other language it would be elementName() or some such function.

    so
    for $b in document("labrep.xml")//presentation
    for $a in $b//*[@*="ByContract"]
    return
    <theAttribute>
    {$a/text(), $a/@*}
    </theAttribute>

    I could somehow return "tabletag" if it was found in element <tabletag
    id="ByContract">...</tabletag>
     
    Jeff Kish, Oct 19, 2004
    #4

  5. > I would guess in some other language it would be elementName() or some such function.


    name() or local-name() if you want to ignore namespace prefixes)

    This is all Xpath 1, not really anything new in Xquery, although that is
    true of most of Xquery, really.

    David
     
    David Carlisle, Oct 19, 2004
    #5
  6. Jeff Kish

    Jeff Kish Guest

    On Tue, 19 Oct 2004 13:59:00 GMT, David Carlisle <> wrote:

    >
    >> I would guess in some other language it would be elementName() or some such function.

    >
    >name() or local-name() if you want to ignore namespace prefixes)
    >
    >This is all Xpath 1, not really anything new in Xquery, although that is
    >true of most of Xquery, really.
    >
    >David

    Thanks. I'm still coming up to speed, and it is hard to find things out
    because there is so much information.. I just need to study example after
    example, and look over the docs.

    thank you once again
    Jeff Kish
     
    Jeff Kish, Oct 19, 2004
    #6
  7. Jeff Kish

    Jeff Kish Guest

    On Tue, 19 Oct 2004 13:59:00 GMT, David Carlisle <> wrote:

    >
    >> I would guess in some other language it would be elementName() or some such function.

    >
    >name() or local-name() if you want to ignore namespace prefixes)
    >
    >This is all Xpath 1, not really anything new in Xquery, although that is
    >true of most of Xquery, really.
    >
    >David

    I must be getting old in my thick age.
    I don't seem to be getting anything from this, and I expected the name of the
    element, so maybe you could tell me how I bolluxed up either the syntax, use,
    or if my expectations are wrong...? I thought name($b) would return the
    element name, i.e. "<dialog>" in the first case, and some other in the next
    two.

    for $b in document("labrep.xml")//*[@*="ByWorkord"]
    return
    <temp>{$b/@*, name($b), local-name($b), namespace-uri($b)}</temp>

    returns this data


    <temp id="ByWorkord"
    label="label1"
    mboname="MBO1"
    width="900"
    height="380">dialog dialog </temp>,
    <temp id="byworkorder_time_table"
    label="label2"
    parentmbo="MBO2"
    relationship="RELATIONSHIP1"
    </temp>,
    <temp id="aclaborwo"
    label="label3"
    parentdatasrc="datasrc1"
    relationship="RELATIONSHIP2"
    dialog </temp>



    Thanks
    Jeff Kish
     
    Jeff Kish, Oct 20, 2004
    #7
  8. Jeff Kish <> writes:

    > On Tue, 19 Oct 2004 13:59:00 GMT, David Carlisle <> wrote:
    >
    > >
    > >> I would guess in some other language it would be elementName() or some such function.

    > >
    > >name() or local-name() if you want to ignore namespace prefixes)
    > >
    > >This is all Xpath 1, not really anything new in Xquery, although that is
    > >true of most of Xquery, really.
    > >
    > >David

    > I must be getting old in my thick age.
    > I don't seem to be getting anything from this, and I expected the name of the
    > element, so maybe you could tell me how I bolluxed up either the syntax, use,
    > or if my expectations are wrong...? I thought name($b) would return the
    > element name, i.e. "<dialog>" in the first case, and some other in the next
    > two.
    >


    > for $b in document("labrep.xml")//*[@*="ByWorkord"]
    > return
    > <temp>{$b/@*, name($b), local-name($b), namespace-uri($b)}</temp>
    >
    > returns this data
    >
    >
    > <temp id="ByWorkord"
    > label="label1"
    > mboname="MBO1"
    > width="900"
    > height="380">dialog dialog </temp>,
    > <temp id="byworkorder_time_table"
    > label="label2"
    > parentmbo="MBO2"
    > relationship="RELATIONSHIP1"
    > </temp>,
    > <temp id="aclaborwo"
    > label="label3"
    > parentdatasrc="datasrc1"
    > relationship="RELATIONSHIP2"
    > dialog </temp>
    >
    >
    >
    > Thanks
    > Jeff Kish



    well without the input I can only guess, but teh name of an element
    thatuses a start tag of <dialog> is dialog not <dialog> and local-name
    is the same if you are not using namespaces.
    I would guess that the first temp above is the correct output.
    You generae a temp element node, copy the attribute nodes (which will
    appear as attributes in the start tag when serialised) then you
    {$b/@*,
    then you make a sequence of three strings
    name($b), local-name($b), namespace-uri($b)
    when you linearise such a sequence you put spaces between each string so
    you end up with

    dialog dialog

    The second <temp< element is not well formed, presumably a cut and paste
    wrong.

    David
     
    David Carlisle, Oct 20, 2004
    #8
  9. Jeff Kish

    Jeff Kish Guest

    <snip>
    >well without the input I can only guess, but teh name of an element
    >thatuses a start tag of <dialog> is dialog not <dialog> and local-name
    >is the same if you are not using namespaces.
    >I would guess that the first temp above is the correct output.
    >You generae a temp element node, copy the attribute nodes (which will
    >appear as attributes in the start tag when serialised) then you
    >{$b/@*,
    >then you make a sequence of three strings
    >name($b), local-name($b), namespace-uri($b)
    >when you linearise such a sequence you put spaces between each string so
    >you end up with
    >
    >dialog dialog
    >
    >The second <temp< element is not well formed, presumably a cut and paste
    >wrong.
    >
    >David

    Thanks.. I understand a bit more now.
    Well, if you have any more time...

    I want to see if I can get the element name (i.e. "book" in this case) of the
    elements with any attribute of value "1992".

    I'm getting the entire element <book> in all its glory.. I'd like to just get
    the "name" of the element that has the attributes, not of its child elements
    also.


    If I use the input below (books.xml)

    and I I submit this query

    for $b in document("books.xml")//*[@*="1992"]
    return
    <temp>{$b/@*, $b/.[name()]}</temp>


    {-- results in this output
    <temp year="1992">
    <book year="1992">a1992<title>Advanced Programming in the UNIX
    Environment</title>
    <author>
    <last>Stevens</last>
    <first>W.</first>
    </author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
    </book>
    </temp>
    --}






    books.xml:

    <bib>
    <book year="1994">a1994 <title>TCP/IP Illustrated</title>
    <author>
    <last>Stevens</last>
    <first>W.</first>
    </author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
    </book>
    <book year="1992">a1992<title>Advanced Programming in the UNIX
    Environment</title>
    <author>
    <last>Stevens</last>
    <first>W.</first>
    </author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
    </book>
    <book year="2000">a2000<title>Data on the Web</title>
    <author>
    <last>Abiteboul</last>
    <first>Serge</first>
    </author>
    <author>
    <last>Buneman</last>
    <first>Peter</first>
    </author>
    <author>
    <last>Suciu</last>
    <first>Dan</first>
    </author>
    <publisher>Morgan Kaufmann Publishers</publisher>
    <price>65.95</price>
    </book>
    <book year="1999">a1999<title>The Economics of Technology and Content
    for Digital TV</title>
    <editor>
    <last>Gerbarg</last>
    <first>Darcy</first>
    <affiliation>CITI</affiliation>
    </editor>
    <publisher>Kluwer Academic Publishers</publisher>
    <price>129.95</price>
    </book>
    </bib>

    Jeff Kish
     
    Jeff Kish, Oct 20, 2004
    #9
  10. you want the name of $b nwhich is name($b) you have $b/.[name()] which
    is the same as $b[name()] which is the same as $b whenever the predicate
    [name()] is true, and since name() returns a string not a boolean it's
    true if the string is non-empty, ie always, as element names are always
    non empty. so itreturns the whole element node b (which includes all
    descendents etc)

    typing even worse than usual as 1 year old "helping"

    David (and Matthew)
     
    David Carlisle, Oct 20, 2004
    #10
  11. Jeff Kish

    Jeff Kish Guest

    On Wed, 20 Oct 2004 22:03:41 GMT, David Carlisle <> wrote:

    >
    >
    >you want the name of $b nwhich is name($b) you have $b/.[name()] which
    >is the same as $b[name()] which is the same as $b whenever the predicate
    >[name()] is true, and since name() returns a string not a boolean it's
    >true if the string is non-empty, ie always, as element names are always
    >non empty. so itreturns the whole element node b (which includes all
    >descendents etc)
    >
    >typing even worse than usual as 1 year old "helping"
    >
    >David (and Matthew)

    Keep those 1 year olds in the loop!
    Thanks
    I'll look it over more
     
    Jeff Kish, Oct 21, 2004
    #11
  12. Jeff Kish

    Jeff Kish Guest

    On Wed, 20 Oct 2004 22:03:41 GMT, David Carlisle <> wrote:

    >
    >
    >you want the name of $b nwhich is name($b) you have $b/.[name()] which
    >is the same as $b[name()] which is the same as $b whenever the predicate
    >[name()] is true, and since name() returns a string not a boolean it's
    >true if the string is non-empty, ie always, as element names are always
    >non empty. so itreturns the whole element node b (which includes all
    >descendents etc)
    >
    >typing even worse than usual as 1 year old "helping"
    >
    >David (and Matthew)

    I see it is case sensitive, i.e. if I search for any element with any attribute named "ByRecord" it
    won't find one named "byRecord". Anyway to get by that? I'm searching the docs also...
    Thanks again. OH.. I have 18, 14, 8 so I know about helping hands!
     
    Jeff Kish, Oct 21, 2004
    #12
    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. chiara
    Replies:
    6
    Views:
    476
    Barry Schwarz
    Oct 6, 2005
  2. Replies:
    5
    Views:
    556
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    579
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    726
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    232
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page