XML transformation with script in the xls
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
string xmlPath = MapPath("BooksWithStyle.xml");
string xslPath = MapPath("Books_with_script.xsl");
XPathDocument xpathDoc = new XPathDocument(xmlPath);
XsltSettings settings = new XsltSettings(false, true);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xslPath, settings, null);
transform.Transform(xpathDoc, null, Response.Output);
}
</script>
<%-- BooksWithStyle.xml
<?xml version='1.0'?>
<bookstore>
<book genre="A">
<title>title 1</title>
<author>
<first-name>A</first-name>
<last-name>B</last-name>
</author>
<price>99.99</price>
</book>
<book genre="B">
<title>title 2</title>
<author>
<first-name>B</first-name>
<last-name>C</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
--%>
<%-- Books_with_script.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:myDiscount="urn:myDiscount">
<msxsl:script language="C#" implements-prefix="myDiscount">
// add cdData tag here
public string ReturnDiscount(string price)
{
decimal priceValue = Convert.ToDecimal(price);
return (priceValue * 15/100).ToString();
}
</msxsl:script>
<xsl:output method="html" />
<xsl:template match="/">
<html>
<title>XSL Transformation</title>
<body>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Price</th>
<th align="left">Calculated Discount</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="myDiscount:ReturnDiscount(price)" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--%>
Related examples in the same category