Pages

Showing posts with label hit xml online. Show all posts
Showing posts with label hit xml online. Show all posts

Tuesday, August 27, 2013

Passing Parameters in XSLT and hitting xml url to get output in html form & store in a string

It is a 4 steps simple and full fletched solution:
my.xml    


<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <projects>
    <S_No>1</S_No>
  </projects>
  <projects>
    <S_No>2</S_No>
  </projects>
  <projects>
    <S_No>3</S_No>
  </projects>
</NewDataSet>



Note: here, 'my.xml' is located 'http://beta.ankit.in//xmlfile/my.xml'. you have to change the path according to your xml location in the xslt.

my.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>
  <xsl:param name="param1" select="$param1" />     <!--get 1st parameter-->
  <xsl:param name="param2" select="$param2" />
     <!--get 2nd parameter--> 
  <xsl:template match="/">
    <xsl:variable name="Site">http://beta.ankit.in/</xsl:variable>   
    <xsl:variable name="Path" select="string(concat($Site,'/xmlfile/my.xml'))"/>

    <xsl:for-each select="document($Path)/NewDataSet/projects">
      <xsl:variable name="fullname" select="string(concat($param1,$param2))"/>
      <div>
        First Name:<xsl:value-of select="$param1"/>
        Last Name:<xsl:value-of select="$param2"/>
      </div>
      <div>
        Full Name:<xsl:value-of select="$fullname"/>
      </div>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>


base.xml 


<?xml version="1.0" encoding="utf-8" ?>
<a>
  <aa></aa>
</a>



Note: this is a supportive xml, just copy it. it will be used at the time of calling.

Default.aspx
  


 private string GetResponseFromXslt(string xsltfile)
    {
        string str = string.Empty;
        try
        {
            XsltArgumentList argList = new XsltArgumentList();
            argList.AddParam("param1", String.Empty, "Ankit");    // passing 1st parameter
            argList.AddParam("param2", String.Empty, "Jaiswal");   // passing 2nd parameter
            XmlReader reader = XmlReader.Create(MapPath("base.xml"));
            XslCompiledTransform objXSLTransform = new XslCompiledTransform();
            XsltSettings _setting = new XsltSettings(true, false);
            objXSLTransform.Load(MapPath("
xsltfile"), _setting, null);
            StringBuilder htmlOutput = new StringBuilder();
            TextWriter htmlWriter = new StringWriter(htmlOutput);
            objXSLTransform.Transform(reader, argList, htmlWriter);
            str = htmlOutput.ToString();
            reader.Close();
            return str.ToString();
        }
        catch (Exception ex) { Response.Write(ex.Message); return str; }
    }



Note: Call this function to get the html response in string "str".

Call this function where u want to show, Like this



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=
GetResponseFromXslt("my.xslt")%>
   or  <%Response.Write(GetResponseFromXslt("my.xslt")); %>
    </div>
    </form>
</body>
</html>