Welcome to WebmasterWorld Guest from 23.20.193.33

Forum Moderators: open

Message Too Old, No Replies

XSL selecting certain values

     
1:39 am on Aug 31, 2004 (gmt 0)

New User

10+ Year Member

joined:Aug 16, 2004
posts:3
votes: 0


I am wanting to change my XSL stylesheet to select all the formattedreportobjects that have a <FormattedReportObject type="CTFormattedField" Type="string" FieldName="{VhStock.MAKE}"> <value> that = Mitsubishi.

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>


Thanks for your time
3:42 pm on Sept 7, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:June 11, 2003
posts:417
votes: 0


poisontheused,
I'm new to xml - so there may be a different method than this!

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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members