Choosing right element

A

anitawa

Hi. I have a dilemma. Here is my xml.

<time>
<hour>1</hour>
<minute>0</minute>
</time>

<time timezone="PST">
<hour>1</hour>
<minute>15</hour>
</time>

<time timezone="EST">
<hour>2</hour>
<minute>20</minute>
</time>

<time timezone="TOK">
<hour>15</hour>
<minute>45</minute>
</time>

I want to choose only one of these <time> depending on system timezone
(variable $tz). If there is no system time defined, then it would
choose the first element with no timezone. Here is my xslt, but its
not working correctly.

<xsl:choose>
<xsl:when test="@timezone=$tz">
<xsl:value-of select="."/>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="."/>
</xsl:eek:therwise>
</xsl:choose>
 
J

Joseph Kesselman

<xsl:when test="@timezone=$tz">
<xsl:value-of select="."/>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="."/>
</xsl:eek:therwise>
</xsl:choose>

What you've said is "If the current node's timezone attribute has the
desired value, output the node's value. If it doesn't, then still output
the node's value." Not what you wanted. Especially since value-of won't
do what you want either; it will output the concatenated text content,
which will be something like
1
15
with line breaks before and after, and with the indentation.


What you wanted was "If there is a <time> element which has the right
timezone output it, otherwise output the one with no timezone". You
didn't show us the context, but assuming that you're starting from the
parent of the <time> elements one way to write this would be:

<xsl:choose>
<xsl:when test="time[@timezone=$tz]">
<xsl:apply-templates select="time[@timezone=$tz]"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates select="time[@timezone='']"/>
</xsl:eek:therwise>
</xsl:choose>

Note that here I'm also assuming you want to apply a template to the
<time> elements in order to format their content a bit more usefully.

Hope that helps.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top