Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Friday, December 9, 2011

XSLT - Numeric Checking

Hi Friends,

Here I would like to say about XSLT programming.
The example mentioned below is for finding the numeric values contained in an xml file.

For testing this example you need two files.

1. XML file its name should be "Numbers.xml"
2. XSL file its name should be "NumberCheck.xsl"

Copy these files to a folder and open the xml file in a browser.

"Numbers.xml"
---------------------
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="NumberCheck.xsl" ?>
<Numbers>
<Number>
<Value>11</Value>
</Number>
<Number>
<Value>12</Value>
</Number>
<Number>
<Value>DD</Value>
</Number>
<Number>
<Value>23</Value>
</Number>
<Number>
<Value>33</Value>
</Number>
<Number>
<Value>FF</Value>
</Number>
<Number>
<Value>VV</Value>
</Number>
</Numbers>

"NumberCheck.xsl"
---------------------------
<?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="Numbers">
<TABLE>
<xsl:for-each select="Number">
<xsl:choose>
<xsl:when test='number(Value)'>
<TR>
<TD>
I am a numeric value : <xsl:value-of select="Value" />
</TD>
</TR>
</xsl:when>
<xsl:otherwise>
<TR>
<TD>
I am not a numeric value : <xsl:value-of select="Value" />
</TD>
</TR>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>

In this example the xslt function <xsl:when test='number(Value)'> will help you to find the given value is a numeric or not

its out put will be like this

I am a numeric value : 11
I am a numeric value : 12
I am not a numeric value : DD
I am a numeric value : 23
I am a numeric value : 33
I am not a numeric value : FF
I am not a numeric value : VV

Friday, October 29, 2010

Format Excel file using XSLT, XslCompiledTransform

See the sample code for creating an Excel file formatted with the help of XSLT file.
------------------------------------------------------------------------------------


private void button1_Click(object sender, EventArgs e)
{
ExportToExcel(GetDataSet(), "Employees.xslt", "c:\\Employees.xls");
}

Method to create a dataset for writing to excel
---------------------------------------------------------

private DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dtEmployees = new DataTable("Employees");
DataColumn colEmpID = new DataColumn("EmpID");
DataColumn colEmpName = new DataColumn("EmpName");
DataColumn colAge = new DataColumn("Age");
DataColumn colLocation = new DataColumn("Location");
DataColumn colDesignation = new DataColumn("Designation");
dtEmployees.Columns.AddRange(new DataColumn[] { colEmpID, colEmpName, colAge, colLocation, colDesignation });

DataRow dr = dtEmployees.NewRow();
dr[colEmpID] = "E001";
dr[colEmpName] = "George";
dr[colAge] = "45";
dr[colLocation] = "USA";
dr[colDesignation] = "PM";
dtEmployees.Rows.Add(dr);

dr = dtEmployees.NewRow();
dr[colEmpID] = "E002";
dr[colEmpName] = "James";
dr[colAge] = "30";
dr[colLocation] = "INDIA";
dr[colDesignation] = "TL";
dtEmployees.Rows.Add(dr);

dr = dtEmployees.NewRow();
dr[colEmpID] = "E002";
dr[colEmpName] = "Mary";
dr[colAge] = "35";
dr[colLocation] = "UK";
dr[colDesignation] = "Developer";
dtEmployees.Rows.Add(dr);

ds.Tables.Add(dtEmployees);
return ds;
}


Method to create a XSLT formatted excel file
---------------------------------------------------------

public bool ExportToExcel(DataSet ds, string xsltPath, string pathToSave)
{
try
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.LoadXml(ds.GetXml());
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(xsltPath);
StreamWriter strWriter = new StreamWriter(pathToSave);
xsl.Transform(xmlDoc, null, strWriter);
strWriter.Flush();
return true;
}
catch (Exception ex)
{
throw ex;
}
}


Save the below mentioned XSLT in a file with name "Employees.xslt" in "C" drive to run the sample
---------------------------------------------------------

<xsl:stylesheet version="1.0"

xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ms="urn:schemas-microsoft-com:xslt"
>
<xsl:template match="NewDataSet">
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="s21">
<Font ss:FontName="Arial" ss:Size="10" ss:Bold="1"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s22">
<Font ss:FontName="MS Sans Serif" ss:Size="10"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s25">
<Font ss:Color="Red" ss:FontName="MS Sans Serif" ss:Size="10"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet>
<xsl:attribute name="ss:Name">Employees List</xsl:attribute>
<Table>
<Column ss:Width="50"/>
<Column ss:Width="50"/>
<Column ss:Width="30"/>
<Column ss:Width="90"/>
<xsl:apply-templates select="NewDataSet"/>
<Row>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Name</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Employee ID</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Age</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Location</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Designation</Data>
</Cell>
</Row>
<xsl:for-each select="Employees">
<Row>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="EmpID"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="EmpName"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Age"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Location"/>
</Data>
</Cell>
<xsl:choose>
<xsl:when test="Designation='PM'">
<Cell ss:StyleID="s25">
<Data ss:Type="String">
<xsl:value-of select="Designation"/>
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Designation"/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</Row>
</xsl:for-each>
</Table>
</Worksheet>
</Workbook>
</xsl:template>
</xsl:stylesheet>

Sunday, February 21, 2010

Conditional Loop In XSLT

XML
--------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xslt" ?>
<Root>
<Title>Looping function example</Title>
<Limit>10</Limit>
</Root>


XSLT
----------------
<?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>
<head>
<title> <xsl:value-of select="Root/Title" /> </title>
</head>
<body>
<!--CALLING FUNCTION -->

<h3><xsl:value-of select="Root/Title" /></h3>
<xsl:call-template name="LoopingFunc">
<xsl:with-param name="limit" select="Root/Limit" />
</xsl:call-template>
</body>
</html>
</xsl:template>
<!--LOOPING FUNCTION -->

<xsl:template name="LoopingFunc">
<xsl:param name="limit"/>
<xsl:if test="$limit > 0">
<h5>
<xsl:value-of select="$limit"/>
</h5>
<xsl:call-template name="LoopingFunc">
<xsl:with-param name="limit" select="$limit - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


Out put
----------------
Looping function example
10
9
8
7
6
5
4
3
2
1