Tuesday, 11 March 2014

One xsl and multipale xml read , direct xml read in xsl

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;

public partial class xmlxsl : System.Web.UI.UserControl
{
    public string xmlfile = "";
    public string xslfile = "";
    public string xslxmlfile = "";
    public string xslxmltxt = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        string myXmlFile = Server.MapPath(xmlfile);
        XPathDocument myXPathDoc = new XPathDocument(myXmlFile);
        XslCompiledTransform myXslTrans = new XslCompiledTransform();
        string myStyleSheet = Server.MapPath(xslfile);
        XsltSettings xs = new XsltSettings(true, true);
        XmlUrlResolver xur = new XmlUrlResolver();
        myXslTrans.Load(myStyleSheet, xs, xur);

        myXslTrans.Transform(myXmlFile, Server.MapPath(xslxmlfile));
        string strHTML = GetHTML(Server.MapPath(xslxmlfile)); ;
        strHTML = strHTML.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
        divHTML.InnerHtml = strHTML;


    }
    public string GetHTML(string strURL)
    {
        string strResult = string.Empty;
        try
        {
            WebResponse objResponse;
            WebRequest objRequest = HttpWebRequest.Create(strURL);
            objResponse = objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                strResult = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch (Exception ex)
        {
            // Response.Write(ex.ToString());
        }
        return strResult;
    }
}

-----------------------------------xsl--------------------------------
<?xml version="1.0" encoding='utf-8'?>
<xsl:stylesheet xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title> XSLT with XML included </title>
      </head>
      <body style="background-color:lightblue;color:green">
        <table cellSpacing="0" border="1" cellPadding="2">
          <!-- Set Variables -->
          <xsl:variable name="external">
            <xsl:copy-of select="/test/child2.xml"/>
          </xsl:variable>
          <!-- Process Data Start -->
          <xsl:for-each select="//UK_Products_Pipeline/LastFinishCode">
            <tr>
              <xsl:if test="SiteName ='MWB'">
                <td>
                  <xsl:value-of select="SiteName"/>
                </td>
                <td>
                  <xsl:value-of select="LastFinishCode"/>
                </td>
                <td>
                  <xsl:value-of select="Numbers"/>
                </td>
              </xsl:if>
            </tr>
          </xsl:for-each>
          <!-- Process File Data Start -->
          <xsl:call-template name="ExternalData">
         
            <xsl:with-param name="data" select="$external"/>
          </xsl:call-template>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="ExternalData">
 
    <xsl:param name="data"/>
   
    <xsl:variable name="external">
      <xsl:copy-of select="/test/child1.xml"/>
    </xsl:variable>
    <table cellSpacing="0" border="1" cellPadding="2" style="background-color:white;color:black">
      <tr>
        <td colspan="3" align="center">
          I do see this.
        </td>
      </tr>
      <!-- Process External Data -->
     
      <xsl:for-each select="document('http://localhost:2115/Xml_transform_xslt/test/child2.xml')//UK_Products_Pipeline/LastFinishCode">
        <tr>
          <td colspan="3" align="center">
            <xsl:value-of select="SiteName"/>
          </td>
        </tr>
        <tr>
          <xsl:if test="SiteName ='UK'">
            <td>
              <xsl:value-of select="SiteName"/>
            </td>
            <td>
              <xsl:value-of select="LastFinishCode"/>
            </td>
            <td>
              <xsl:value-of select="Numbers"/>
            </td>
          </xsl:if>
        </tr>
      </xsl:for-each>
    
    </table>
  </xsl:template>
</xsl:stylesheet>
-------------------xml 1----------
<?xml version="1.0" encoding="utf-8"?>
<UK_Products_Pipeline>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>Agent Logout</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>Busy</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>BW Sale</LastFinishCode>
    <Numbers>1 koli</Numbers>
  </LastFinishCode>
</UK_Products_Pipeline>
------------------xml 2-----------------
<?xml version="1.0" encoding="utf-8"?>

<UK_Products_Pipeline>
  <LastFinishCode>
    <SiteName>MWB pramodhanmant</SiteName>
    <LastFinishCode>Bearer Capability Not Presently Authorized (ISDN Cause Code 57)</LastFinishCode>
    <Numbers>1 hanmant</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>MWB</SiteName>
    <LastFinishCode>Confirmed Booking</LastFinishCode>
    <Numbers>1 hanmant</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>MWB</SiteName>
    <LastFinishCode>Lost</LastFinishCode>
    <Numbers>1 hanmant</Numbers>
  </LastFinishCode>
</UK_Products_Pipeline>

No comments:

Post a Comment