Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Choosing right element

Reply
Thread Tools

Choosing right element

 
 
anitawa
Guest
Posts: n/a
 
      05-07-2007
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>
<xsltherwise>
<xsl:value-of select="."/>
</xsltherwise>
</xsl:choose>

 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      05-07-2007
> <xsl:choose>
> <xsl:when test="@timezone=$tz">
> <xsl:value-of select="."/>
> </xsl:when>
> <xsltherwise>
> <xsl:value-of select="."/>
> </xsltherwise>
> </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>
<xsltherwise>
<xsl:apply-templates select="time[@timezone='']"/>
</xsltherwise>
</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.




--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
advice on choosing right control Mark Siffer ASP .Net 1 06-17-2004 06:26 AM
Need help choosing the right router. Robert Le Feve Cisco 3 11-19-2003 02:31 AM
Re: Choosing the right training company Deane MCSE 0 09-05-2003 07:58 PM
Re: Choosing the right training company S. O'Brien MCSE 0 09-05-2003 01:58 PM



Advertisments