How to do XSLT for this?

M

mali djuro

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
 
C

Colin Mackenzie

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>
 
K

Klaus Johannes Rusch

mali said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top