Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stylesheet if statement?

Status
Not open for further replies.

mruncola

Programmer
Joined
Jul 1, 2004
Messages
2
Location
US
I am new to xml / xsl and I am trying to use a xsl stylesheet to create a table. I have a <resultNumber> field in my xml document and i would like to test if this result field is an increment of any digit TBD. Once it gets to that increment digit then i would like to add a new line to the table.

It is currently creating a horizontal table.

Right now I would like it to create a 2 column by X rows table. I would like to be able to change the 2 to a 3 or 4 so i can generate multi column tables also.

THANKS!

---- XML DOC ---

<Results xmlns:xsi=" xsi:noNamespaceSchemaLocation="../xmlschemas/results.xsd" total="4">
<Product>
<resultNumber>1</resultNumber>
<name>A</name>
<number>001</number>
</Product>
<Product>
<resultNumber>2</resultNumber>
<name>B</name>
<number>411</number>
</Product>
<Product>
<resultNumber>3</resultNumber>
<name>C</name>
<number>051</number>
</Product>
<Product>
<resultNumber>4</resultNumber>
<name>D</name>
<number>201</number>
</Product>
</Results>

--- XSL DOC ---

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Results">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<xsl:for-each select="Product">
<td valign="top" width="130">
<div style="width: 130px; margin: 3px 10px;">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:text>proddetail.jsp?productid=</xsl:text>
<xsl:value-of select="number"/>
</xsl:attribute>
<xsl:value-of select="name" />
</xsl:element>
</div>
</div>
</td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
 
I figured it out with help from this article.


This lets me set as many columns as i want by changing two numbers.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match ="/" >
<html>
<table border="0" cellpadding="0" cellspacing="0">
<xsl:for-each select ="/Results/Product[position() mod 2 = 1]" >
<tr>
<xsl:apply-templates select =". | following-sibling::Product[position() &lt; 2]/." />
</tr>
</xsl:for-each>
</table>
</html>
</xsl:template>

<xsl:template match ="Product" >
<td valign="top" width="130">
<div style="width: 130px; margin: 3px 10px;">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:text>proddetail.jsp?productid=</xsl:text>
<xsl:value-of select="number"/>
</xsl:attribute>
<xsl:value-of select="name" />
</xsl:element>
</div>
</div>
</td>
</xsl:template>

</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top