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

F

FLEB

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!
 
R

Richard Tobin

FLEB said:
<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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

FLEB said:
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]
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top