Thursday, September 24, 2009

Using Counters in xml with the C# code using the msxsl namespace

How to use Counters in xml with the C# code using the msxsl namespace.


Suppose this is the XML

<detail>
<plan>
<plan-code>CWTM0029BUCL30 </plan-code>
<plan-desc> Plan </plan-desc>
<ban-text>Ban :</ban-text>
<ban>40000602884 </ban>
<prorata-monthly-text>Approx pro-rata charge </prorata-monthly-text>
<prorata-call-text>Approx pro-rata included call </prorata-call-text>
<prorata-comment-1>A</prorata-comment-1>
<prorata-comment-2>B</prorata-comment-2>
<prorata-comment-3>C</prorata-comment-3>
<handset-code>NOKE63STBL </handset-code>
<handset-desc>Nokia </handset-desc>
<handset-serial>359319 </handset-serial>
<bundles>
<bundled-product>EXTENDWARR </bundled-product>
<bundled-desc>EWDIG2G </bundled-desc>
<plan-ew-imei>EW IMEI </plan-ew-imei>
<plan-ew-mobile>EW Mobile No : </plan-ew-mobile>
<plan-ew-expiry>EW Expiry Date </plan-ew-expiry>
</bundles>
<connection-type> Upgrade </connection-type>
<mobile-text>Mobile :</mobile-text>
<mobile>0666666</mobile>
<plan-cont-inc-gst> 0.00</plan-cont-inc-gst>
</plan>

<outright>
<qty>111111</qty>
<product>CJPOWERSD2GB </product>
<stock-description>Power SD2</stock-description>
<stock-desc1>Aftermarket</stock-desc1>
<stock-desc2>2GB</stock-desc2>
<unit-price>16.95</unit-price>
<discount>15.41</discount>
<line-amt-ex-gst>0</line-amt-ex-gst>
<line-gst-amt>0</line-gst-amt>
<line-amt-inc-gst>0</line-amt-inc-gst>
<item-cost>9</item-cost>
<line-cost>9</line-cost>
<gp>-9</gp>
</outright>
<ordernotes>
</ordernotes>
</detail>



This is the XSLT where I have written a code where the variable
act as a Counter.



<?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:user="urn:my-scripts">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<msxsl:script language="C#" implements-prefix="user"><![CDATA[
public int ctr = 0 ;
public int AddCounter()
{
ctr = ctr +1;
return ctr;
}
]]>
</msxsl:script>
<xsl:template match="/">
<xsl:variable name="vvr_ctrMain" select="'0'"/>
<html>
<body>
<table border="1">
<xsl:for-each select="detail">
<tr>

<td>
<table border="1">
<tr>
<td>Line</td>
<td>qty</td>
<td>product</td>
<td>unit-price</td>
<td>discount</td>
<td>line-amt-ex-gst</td>
<td>line-gst-amt</td>
<td>line-amt-inc-gst</td>
</tr>
<xsl:for-each select="outright">
<tr>
<td>
<xsl:value-of select="user:AddCounter()"/>
</td>
<td>
<xsl:value-of select="qty"/>
</td>
<td>
<xsl:value-of select="product"/>
</td>
<td>
<xsl:value-of select="unit-price"/>
</td>
<td>
<xsl:value-of select="discount"/>
</td>
<td>
<xsl:value-of select="line-amt-ex-gst"/>
</td>
<td>
<xsl:value-of select="line-gst-amt"/>
</td>
<td>
<xsl:value-of select="line-amt-inc-gst"/>
</td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
<tr>
<td>
<table width="40%" border="1">
<tr>

<td>
<xsl:value-of select="user:AddCounter()"/>
</td>

<td>stock Description</td>
<td>
<xsl:value-of select="outright/stock-description"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="user:AddCounter()"/>
</td>
<td>plan-code</td>
<td>
<xsl:value-of select="plan/plan-code"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="user:AddCounter()"/>
</td>
<td>bundled-product</td>
<td>
<xsl:value-of select="plan/bundles/bundled-product"/>
</td>
</tr>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>




This would be the output


<?xml version="1.0" encoding="utf-8"?>
<html xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
<body>
<table border="1">
<tr>
<td>
<table border="1">
<tr>
<td>Line</td>
<td>qty</td>
<td>product</td>
<td>unit-price</td>
<td>discount</td>
<td>line-amt-ex-gst</td>
<td>line-gst-amt</td>
<td>line-amt-inc-gst</td>
</tr>
<tr>
<td>1</td>
<td>111111</td>
<td>CJPOWERSD2GB</td>
<td>16.95</td>
<td>15.41</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="40%" border="1">
<tr>
<td>2</td>
<td>stock Description</td>
<td>Power SD2</td>
</tr>
<tr>
<td>3</td>
<td>plan-code</td>
<td>CWTM0029BUCL30</td>
</tr>
<tr>
<td>4</td>
<td>bundled-product</td>
<td>EXTENDWARR</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

No comments:

Post a Comment