Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   [XSL] Finding "the node with the same @some_attribute value as mine" (http://www.velocityreviews.com/forums/t167750-xsl-finding-the-node-with-the-same-some_attribute-value-as-mine.html)

FLEB 08-29-2004 03:00 AM

[XSL] Finding "the node with the same @some_attribute value as mine"
 
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
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com

Richard Tobin 08-29-2004 10:31 AM

Re: [XSL] Finding "the node with the same @some_attribute value as mine"
 
In article <1ejznzm0mdyld$.x02mq8ibgmi2$.dlg@40tude.net>,
FLEB <soon.the.sp@mmers.and.evil.ones.will.bow-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:of>.

-- Richard

=?ISO-8859-1?Q?J=FCrgen_Kahrs?= 08-29-2004 10:39 AM

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]
}



All times are GMT. The time now is 07:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.