Choosing right element

Discussion in 'XML' started by anitawa, May 7, 2007.

  1. anitawa

    anitawa Guest

    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>
     
    anitawa, May 7, 2007
    #1
    1. Advertising

  2. > <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>


    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.




    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, May 7, 2007
    #2
    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. Mark Siffer

    advice on choosing right control

    Mark Siffer, Jun 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    334
    Trevor Benedict R
    Jun 17, 2004
  2. shesh

    Choosing the right platform

    shesh, Jul 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    jasonkester
    Jul 18, 2005
  3. Thomas Weholt \( PRIVAT \)

    Choosing the right database-system for a small project

    Thomas Weholt \( PRIVAT \), Jun 24, 2003, in forum: Python
    Replies:
    2
    Views:
    1,085
    Thomas Weholt \( PRIVAT \)
    Jun 24, 2003
  4. Carsten Gehling

    Choosing the right framework

    Carsten Gehling, Jul 16, 2003, in forum: Python
    Replies:
    9
    Views:
    311
    Ville Vainio
    Nov 27, 2003
  5. Carsten Gehling

    SV: Choosing the right framework

    Carsten Gehling, Jul 16, 2003, in forum: Python
    Replies:
    0
    Views:
    298
    Carsten Gehling
    Jul 16, 2003
Loading...

Share This Page