[XSL] Finding "the node with the same @some_attribute value as mine"

Discussion in 'XML' started by FLEB, Aug 29, 2004.

  1. FLEB

    FLEB Guest

    Okay, so I've got this XML:

    <qa>
    <questionset>
    <question name="yourname">What is your name?</question>
    <question name="yourquest">What is your quest?</question>
    <question name="favcolor"> What is your favorite color?</question>
    </questionset>
    <answerset time="some_unique_time">
    <answer question="yourname">FLEB the Amazing</answer>
    <answer question="yourquest">To get some decent XHTML</answer>
    <answer question="favcolor">Dark white</answer>
    </answerset>
    <!-- more answersets... -->
    </qa>

    In use, <questionset> is unique to its <qa> container (there may be other
    <qa>s, but any given <qa> will have only one <questionset>). There will,
    however, be a lot of different <answerset>s. I want to transform this so I
    get the full question with each answer. I just can't think of a way to say
    "The question element with the attribute "name", matching THE CURRENT
    ELEMENT's "question" value."

    I've tried, for instance, looping through the <answer> elements, and using
    something like:

    <xsl:value-of select="../../questionset/question[@name=@answer]">

    but this (as it naturally should) tries to compare the question's @name to
    its own @answer. I'm stuck. I also don't want to use IDs in a DTD, because
    I might have multiple <questions> with the same id, but under a different
    pile of heirarchy. Is there any way to "reset" back to the start of the
    path in order to retrieve the current element's @question value? Or (more
    likely) am I thinking about this all wrong?

    Thanks for any help!

    --
    -- Rudy Fleminger
    -- -down-to.us
    (put "Hey!" in the Subject line for priority processing!)
    -- http://www.pixelsaredead.com
     
    FLEB, Aug 29, 2004
    #1
    1. Advertising

  2. In article <1ejznzm0mdyld$.x02mq8ibgmi2$>,
    FLEB <-down-to.us> wrote:

    ><xsl:value-of select="../../questionset/question[@name=@answer]">
    >
    >but this (as it naturally should) tries to compare the question's @name to
    >its own @answer.


    Use the current() function, e.g. [@name=current()/@answer], or set a
    variable to the @answer value outside the <value-xsl:eek:f>.

    -- Richard
     
    Richard Tobin, Aug 29, 2004
    #2
    1. Advertising

  3. Re: [XSL] Finding "the node with the same @some_attribute value asmine"

    FLEB the Amazing wrote:
    > Okay, so I've got this XML:
    >
    > <qa>
    > <questionset>
    > <question name="yourname">What is your name?</question>
    > <question name="yourquest">What is your quest?</question>
    > <question name="favcolor"> What is your favorite color?</question>
    > </questionset>
    > <answerset time="some_unique_time">
    > <answer question="yourname">FLEB the Amazing</answer>
    > <answer question="yourquest">To get some decent XHTML</answer>
    > <answer question="favcolor">Dark white</answer>
    > </answerset>
    > <!-- more answersets... -->
    > </qa>


    Interesting problem. I am sorry to say that I have
    no solution in XSL (which you were asking for).
    But the problem was an interesting exercise for me
    to find out where the limits of xmlgawk are.

    > In use, <questionset> is unique to its <qa> container (there may be other
    > <qa>s, but any given <qa> will have only one <questionset>). There will,
    > however, be a lot of different <answerset>s. I want to transform this so I
    > get the full question with each answer. I just can't think of a way to say
    > "The question element with the attribute "name", matching THE CURRENT
    > ELEMENT's "question" value."


    This is the output I get from the attached xmlgawk script:

    q: What is your favorite color? a: Dark white
    q: What is your name? a: FLEB the Amazing
    q: What is your quest? a: To get some decent XHTML


    # qa.awk
    # comp.text.xml 2004-08-29
    # Access to same attribute in different element.
    # JK 2004-08-29

    BEGIN { XMLMODE=1 }

    XMLSTARTELEM == "question" { q=XMLATTR["name"] }
    XMLSTARTELEM == "answer" { a=XMLATTR["question"] }

    XMLENDELEM { q=a=0 }

    XMLCHARDATA && q { qset[q] = $0}
    XMLCHARDATA && a { aset[a] = $0}

    END {
    for (q in qset)
    print "q:", qset[q], "a:", aset[q]
    }
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Aug 29, 2004
    #3
    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. Kevin Flood
    Replies:
    0
    Views:
    1,027
    Kevin Flood
    Sep 8, 2004
  2. Kevin Dean
    Replies:
    4
    Views:
    519
    Kevin Dean
    Sep 11, 2003
  3. Victor Engmark
    Replies:
    4
    Views:
    738
    Piet Blok
    Jun 1, 2004
  4. Gadrin77
    Replies:
    3
    Views:
    437
    Gadrin77
    May 7, 2004
  5. Replies:
    1
    Views:
    3,622
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page