How to do XSLT for this?

Discussion in 'XML' started by mali djuro, Jul 3, 2003.

  1. mali djuro

    mali djuro Guest

    Hi, all!

    i am newbie in all this stuffs about xsl, so i have few questions for you,
    Please, can you tell me is this possible to do, and if you can please give
    me some guide lines.

    here is problem or whatever:

    i got a following xml file:

    <root>
    <sub>
    <child>
    <name>john</child>
    <phone>1234</phone>
    </child>
    <child>
    <name>mali</name>
    <phone />
    </child>
    ...
    </sub>
    </root>

    i want to show this xml in browser in following way:

    ----------------------------------------------
    | TEST |
    ----------------------------------------------
    | child name | child phone |
    ----------------------------------------------
    | john | 1234 |
    bgColor=blue
    ----------------------------------------------
    |mali | |
    bgcolor=white
    ----------------------------------------------
    ....
    ----------------------------------------------

    problem is that i dont know how to change background colour of <tr>.
    and also how to check is value of element null or there is something?

    please, help!

    thanks
    mali djuro, Jul 3, 2003
    #1
    1. Advertising

  2. Hi,

    your xml is not well formed (name should be closed with name not child)

    colout or a tr, I guess you are saying for every 2nd row bit of an FAQ but
    this will work


    <xsl:template match="/root/sub">
    <table>
    <tbody>
    <xsl:for-each select="child">
    <xsl:choose>
    <xsl:when test="(position()=1) or (position() mod 2 = 1) ">
    <tr bgcolor="blue">
    <td><xsl:value-of select="name"/></td>
    <td>
    <xsl:choose>
    <xsl:when test="phone=''">no phone</xsl:when>
    <xsl:eek:therwise><xsl:value-of select="phone"/></xsl:eek:therwise>
    </xsl:choose>
    </td>
    </tr>
    </xsl:when>
    <xsl:eek:therwise>
    <tr bgcolor="white">
    <td><xsl:value-of select="name"/></td>

    <td>
    <xsl:choose>
    <xsl:when test="phone=''">no phone</xsl:when>
    <xsl:eek:therwise><xsl:value-of select="phone"/></xsl:eek:therwise>
    </xsl:choose>
    </td>
    </tr>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:for-each>
    </tbody>
    </table>
    </xsl:template>





    "mali djuro" <> wrote in message
    news:be0mjf$18c6$...
    > Hi, all!
    >
    > i am newbie in all this stuffs about xsl, so i have few questions for you,
    > Please, can you tell me is this possible to do, and if you can please give
    > me some guide lines.
    >
    > here is problem or whatever:
    >
    > i got a following xml file:
    >
    > <root>
    > <sub>
    > <child>
    > <name>john</child>
    > <phone>1234</phone>
    > </child>
    > <child>
    > <name>mali</name>
    > <phone />
    > </child>
    > ...
    > </sub>
    > </root>
    >
    > i want to show this xml in browser in following way:
    >
    > ----------------------------------------------
    > | TEST |
    > ----------------------------------------------
    > | child name | child phone |
    > ----------------------------------------------
    > | john | 1234 |
    > bgColor=blue
    > ----------------------------------------------
    > |mali | |
    > bgcolor=white
    > ----------------------------------------------
    > ...
    > ----------------------------------------------
    >
    > problem is that i dont know how to change background colour of <tr>.
    > and also how to check is value of element null or there is something?
    >
    > please, help!
    >
    > thanks
    >
    >
    >
    Colin Mackenzie, Jul 3, 2003
    #2
    1. Advertising

  3. mali djuro wrote:

    > i am newbie in all this stuffs about xsl, so i have few questions for you,
    > Please, can you tell me is this possible to do, and if you can please give
    > me some guide lines.
    >
    > here is problem or whatever:
    >
    > i want to show this xml in browser in following way:


    Define templates for each element that output the desired HTML code, and apply
    templates for contained elements. The samples in the XSLT specification, or a
    decent XSLT book such as Michael Kay's or Doug Tidwell's certainly will help,
    too.

    > problem is that i dont know how to change background colour of <tr>.


    Not exactly an XSLT problem but here you go:

    <tr>
    <td bgcolor="red">
    </td>
    <td bgcolor="red">
    </td>
    </tr>

    You can check if the position of your element is even or odd, and use
    different colors accordingly.

    >
    > and also how to check is value of element null or there is something?


    Even for an element without content you will still want to include the <td>,
    you could do something like

    <td>
    <xsl:choose>
    <xsl:when test="phone/text() = ''"> </xsl:when>
    <xsl:eek:therwise><xsl:value-of select="phone/text()" />
    </xsl:choose>
    </td>

    if you want to include a non-breaking space (or some other information, like
    N/A) for empty cells.

    --
    Klaus Johannes Rusch

    http://www.atmedia.net/KlausRusch/
    Klaus Johannes Rusch, Jul 3, 2003
    #3
  4. mali djuro

    mali djuro Guest

    thanks,

    it was typing mistake with name and child


    "Colin Mackenzie" <> wrote in message
    news:be0qia$76n$...
    > Hi,
    >
    > your xml is not well formed (name should be closed with name not child)
    >
    > colout or a tr, I guess you are saying for every 2nd row bit of an FAQ but
    > this will work
    >
    >
    > <xsl:template match="/root/sub">
    > <table>
    > <tbody>
    > <xsl:for-each select="child">
    > <xsl:choose>
    > <xsl:when test="(position()=1) or (position() mod 2 = 1) ">
    > <tr bgcolor="blue">
    > <td><xsl:value-of select="name"/></td>
    > <td>
    > <xsl:choose>
    > <xsl:when test="phone=''">no phone</xsl:when>
    > <xsl:eek:therwise><xsl:value-of select="phone"/></xsl:eek:therwise>
    > </xsl:choose>
    > </td>
    > </tr>
    > </xsl:when>
    > <xsl:eek:therwise>
    > <tr bgcolor="white">
    > <td><xsl:value-of select="name"/></td>
    >
    > <td>
    > <xsl:choose>
    > <xsl:when test="phone=''">no phone</xsl:when>
    > <xsl:eek:therwise><xsl:value-of select="phone"/></xsl:eek:therwise>
    > </xsl:choose>
    > </td>
    > </tr>
    > </xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:for-each>
    > </tbody>
    > </table>
    > </xsl:template>
    >
    >
    >
    >
    >
    > "mali djuro" <> wrote in message
    > news:be0mjf$18c6$...
    > > Hi, all!
    > >
    > > i am newbie in all this stuffs about xsl, so i have few questions for

    you,
    > > Please, can you tell me is this possible to do, and if you can please

    give
    > > me some guide lines.
    > >
    > > here is problem or whatever:
    > >
    > > i got a following xml file:
    > >
    > > <root>
    > > <sub>
    > > <child>
    > > <name>john</child>
    > > <phone>1234</phone>
    > > </child>
    > > <child>
    > > <name>mali</name>
    > > <phone />
    > > </child>
    > > ...
    > > </sub>
    > > </root>
    > >
    > > i want to show this xml in browser in following way:
    > >
    > > ----------------------------------------------
    > > | TEST |
    > > ----------------------------------------------
    > > | child name | child phone |
    > > ----------------------------------------------
    > > | john | 1234 |
    > > bgColor=blue
    > > ----------------------------------------------
    > > |mali | |
    > > bgcolor=white
    > > ----------------------------------------------
    > > ...
    > > ----------------------------------------------
    > >
    > > problem is that i dont know how to change background colour of <tr>.
    > > and also how to check is value of element null or there is something?
    > >
    > > please, help!
    > >
    > > thanks
    > >
    > >
    > >

    >
    >
    mali djuro, Jul 3, 2003
    #4
    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. Stylus Studio
    Replies:
    0
    Views:
    661
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,681
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    623
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    719
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    672
Loading...

Share This Page