Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2011, 04:44 PM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Default Generate ouput depending on subnode value

Hi,

I have to generate an HTML table based on an XML that looks like this:

Code:
<table>
  <tablerow>
    <field name="CONTENT"><value>Some text</value></field>
    <field name="TYPE"><value>NORMAL</value></field>
  </tablerow>
 
  <tablerow>
    <field name="CONTENT"><value>Other text</value></field>
    <field name="TYPE"><value>ERROR</value></field>
  </tablerow>
</table>

I have no idea of how to use XSL to transform it to this output:


Code:
<table>
  <tr>
	<td><font color="BLUE">Some text</font></td>
  </tr>

  <tr>
	<td><font color="RED">Other text</font></td>
  </tr>
</table>

The problem is that all the nodes in the input are called the same way ('field'), and the meaning of the node is based on the attribute 'name'.

Any help will be appreciated

Thanks in advance,
Cristina.
 
Old August 9th, 2011, 04:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well to write a template depending on a particular attribute you'd do something like this:

Code:
<xsl:template match="field[@name='CONTENT']">
  ... do something with fields of type 'content'
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
cristina.macias.614 (August 9th, 2011)
 
Old August 9th, 2011, 07:18 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="table">
 <table>
     <xsl:for-each select="tablerow">
         <tr>
   <xsl:apply-templates select="field[@name='CONTENT']"/>
   </tr>
        </xsl:for-each>
 </table>
    </xsl:template>
 
 <xsl:template match="field[@name='CONTENT']">
  <td><font color="BLUE"><xsl:value-of select="value"/></font></td>
 </xsl:template>
 
 
</xsl:stylesheet>
__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
cristina.macias.614 (August 9th, 2011)
 
Old August 9th, 2011, 07:34 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Default

The problem is that I have to process de nodes 'field' in a diferent way depending of the value of an attribute ('name=TYPE') and also depending of the value of this node (if value is 'NORMAL' or 'ERROR').
 
Old August 9th, 2011, 07:43 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Default

I have tried the code you has post, but don´t use the node:

Quote:
<field name="TYPE"><value>NORMAL</value></field>
to select de color of the font (Blue if '<field name='TYPE'><value>NORMAL...' and red if '<field name='TYPE'><value>ERROR...').
 
Old August 9th, 2011, 07:50 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="table">
 <table>
     <xsl:for-each select="tablerow[field[@name='TYPE']/value[. = 'NORMAL']]">
         <tr>
   <td><font color="BLUE"><xsl:value-of select="field[@name='CONTENT']/value"/></font></td>
   </tr>
        </xsl:for-each>
 
  <xsl:for-each select="tablerow[field[@name='TYPE']/value[. = 'ERROR']]">
         <tr>
   <td><font color="RED"><xsl:value-of select="field[@name='CONTENT']/value"/></font></td>
   </tr>
        </xsl:for-each>
 
 
 </table>
    </xsl:template>
 
 
 
</xsl:stylesheet>
__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
cristina.macias.614 (August 9th, 2011)
 
Old August 9th, 2011, 08:03 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Yeaahhhh¡¡¡¡ It works¡¡¡¡¡

I have another solution I was posting at the same time of yours (also works):


Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" standalone="no"/>



    <xsl:template match="table">

<html><body>

    <table border="1">
      	<xsl:for-each select="tablerow">
       		<tr><td>

			

			<xsl:if test="field[@name='TYPE'] and field='NORMAL'">
				<font color='blue'>
					<xsl:value-of select="field[@name='CONTENT']"/>
				</font>
			</xsl:if>

			<xsl:if test="field[@name='TYPE'] and field='ERROR'">
				<font color='red'>
					<xsl:value-of select="field[@name='CONTENT']"/>
				</font>
			</xsl:if>
			
				


		</td></tr>
 	</xsl:for-each>
    </table>

</body></html>

    </xsl:template>

Thanks a lot¡¡





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Ouput replaces dot as comma ?? elayaraja.s XSLT 2 August 19th, 2008 08:37 AM
Ouput Error phungleon C++ Programming 0 June 7th, 2007 05:53 PM
Wiered ouput exstream ASP.NET 2.0 Basics 2 May 12th, 2006 04:59 PM
how to get values depending on the attribute vidhya XSLT 1 July 8th, 2005 03:55 AM
helpme ! query ouput categorize by group on asp eyesonly Classic ASP Databases 0 January 23rd, 2004 04:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.