[xquery-talk] comparing consequtive elements

Chris Ness cness at soundmotion.co.uk
Fri Jun 2 13:24:27 PDT 2006


andrew welch wrote:

> On 6/2/06, Chris Ness <cness at soundmotion.co.uk> wrote:
>
>> hi all. I'm very new to this game and finding it difficult to find out
>> how to do what I want to do.
>>
>> I want to compare elements (well thier attributes) to look for a
>> particular transition.
>>
>> e.g.
>>
>> <eg N="1">
>>     <a attribute="1"/>
>>     <a attribute="1"/>
>>     <a attribute="2"/>
>>     <a attribute="1"/>
>>     <a attribute="3"/>
>>     <a attribute="2"/>
>>     <a attribute="1"/>
>>     <a attribute="1"/>
>> </eg>
>>
>> and
>>
>> <eg N="2">
>>     <a attribute="1"/>
>>     <a attribute="2"/>
>>     <a attribute="3"/>
>>     <a attribute="1"/>
>>     <a attribute="3"/>
>>     <a attribute="1"/>
>>     <a attribute="1"/>
>>     <a attribute="1"/>
>> </eg>
>>
>> now say I need to look for the transition
>>
>> <a attribute="3"/>
>> <a attribute="2"/>
>>
>> example one will produce a positive and example 2 a negative (a yes/no
>> answer is sufficient at this time)
>>
>> Been searching for hours! tried preceding-sibling but I don't think I
>> know what I'm doing enough.
>
>
> How about:
>
> a[@attribute = '3']/following-sibling::*[1][self::a][@attribute = '2']
>
> cheers
> andrew
>
ah. thanks andrew and david.

 now I have to understand how thats working! all that *[1] and self:: 
stuff means nothing to me yet. the if/then/else makes more sense to me 
though. just

don't want to go asking too many noob questions but this stuff is 
important for us to do before we commit to XML/XQuery. I'll bugger off 
and devour a book after this!

my example was too simplistic. the real stuff looks like this.

<eg>
    <section n="1">
        <a attribute="1"/>
        <a attribute="2"/>
    <section/>
    <section n="2">
        <a attribute="3"/>
        <a attribute="1"/>
    <section/>
    <section n="3">
        <a attribute="3"/>
        <a attribute="3"/>
    <section/>
    <section n="4">
        <a attribute="1"/>
        <a attribute="1"/>
    <section/>
</eg>

say I'm looking for

 <a attribute="3"/>
 <a attribute="1"/>

I need to look accross the <section> divisions too and return the 
section number the transition starts in. so that snippet will produce 2 
and 3.

any replies will be completely analysed! great thanks and no further 
questions!

need a good book for XQuery/Xpath. any recommendations?



More information about the talk mailing list