Forum Moderators: open
At the moment its just outputting all the FormattedReportObjects. Any help would be appreciated.
XML file
<FormattedReportObjects>
<FormattedReportObject type="CTFormattedField" Type="string" FieldName="{VhStock.MAKE}">
<ObjectName>Field81</ObjectName>
<FormattedValue>MITSUBISHI</FormattedValue>
<Value>MITSUBISHI</Value>
</FormattedReportObject>
<FormattedReportObject type="CTFormattedField" Type="string" FieldName="{VhStock.model_name}">
<ObjectName>Field83</ObjectName>
<FormattedValue>MAGNA</FormattedValue>
<Value>MAGNA</Value>
</FormattedReportObject>
</FormattedReportObjects><FormattedReportObjects>
<FormattedReportObject type="CTFormattedField" Type="string" FieldName="{VhStock.MAKE}">
<ObjectName>Field81</ObjectName>
<FormattedValue>DAEWOO</FormattedValue>
<Value>DAEWOO</Value>
</FormattedReportObject>
<FormattedReportObject type="CTFormattedField" Type="string" FieldName="{VhStock.model_name}">
<ObjectName>Field83</ObjectName>
<FormattedValue>LANOS</FormattedValue>
<Value>LANOS</Value>
</FormattedReportObject>
</FormattedReportObjects>
Here is my current XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<basefont face="Verdana" size="2"/>
<body>
<h2>Used Vehicles</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="FormattedReportObjects"><table border="0">
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="FormattedReportObject">
<tr>
<xsl:apply-templates select="Value"/>
</tr>
</xsl:template>
<xsl:template match="Value">
<td bgcolor="#E1E1E1" width="200">
<xsl:if test="parent::FormattedReportObject[@FieldName = '{VhStock.MAKE}']">
<xsl:text>Make</xsl:text>
</xsl:if>
<xsl:if test="parent::FormattedReportObject[@FieldName = '{VhStock.model_name}']">
<xsl:text>Model Name</xsl:text>
</xsl:if>
</td>
<td>
<xsl:value-of select="text()"/>
</td>
</xsl:template>
</xsl:stylesheet>
If I need to select xml elements that contain a particular value I use a "Choose" tag. Below is a sample doc:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="people.xsl"?>
<people>
<person>
<id>1</id>
<firstname>John</firstname>
<surname>Smith</surname>
<age>27</age>
</person>
<person>
<id>2</id>
<firstname>Joe</firstname>
<surname>Blogs</surname>
<age>19</age>
</person>
<person>
<id>3</id>
<firstname>Peter</firstname>
<surname>Smith</surname>
<age>27</age>
</person>
<person>
<id>4</id>
<firstname>Tom</firstname>
<surname>Blogs</surname>
<age>19</age>
</person>
</people>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Family</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Firstname</th>
<th align="left">Surname</th>
</tr>
<xsl:for-each select="people/person">
<xsl:choose>
<xsl:when test="surname='Smith'">
<tr>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="surname"/></td>
</tr>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The "for-each" tag selects all the elements, but the "choose" tag narrows it down to those elements that meet the conditions in its "test" attribute - i.e. surname='Smith'. Note that "test" is part of the tag.
Theres also an optional "<otherwise>" tag associated with the <choose> tag (search for "xml choose when otherwise tutorial" on Google for more info)
Hope this helps.