CS page code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Configuration;
public partial class xsl_convert_html : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
twoxml();
}
public void twoxml()
{
var xslt = new XslCompiledTransform();
xslt.Load(Server.MapPath("~/merge.xslt"), new XsltSettings{EnableDocumentFunction = true}, null );
var xmlr = XmlReader.Create(Server.MapPath("~/A.xml"));
var xmlw = XmlWriter.Create(Server.MapPath("~/result.htm"));
var xslargs = new XsltArgumentList();
xslargs.AddParam("fileName", "", "B.xml");
xslt.Transform(xmlr, xslargs , xmlw);
xmlw.Close();
xmlr.Close();
Response.Redirect("result.htm");
}
}
A.XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Result>
<A>
<Id>A-001</Id>
<Title1>A1 file</Title1>
<Description>This is A1 xml file</Description>
</A>
<A>
<Id>A-002</Id>
<Title1>A2 file</Title1>
<Description>This is A2 xml file</Description>
</A>
<A>
<Id>A-003</Id>
<Title1>A3 file</Title1>
<Description>This is A3 xml file</Description>
</A>
<A>
<Id>A-004</Id>
<Title1>A4 file</Title1>
<Description>This is A4 xml file</Description>
</A>
</Result>
B.XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Result>
<A>
<Id>B-001</Id>
<Title1>B1 file</Title1>
<Description>This is B1 xml file</Description>
</A>
<A>
<Id>B-002</Id>
<Title1>B2 file</Title1>
<Description>This is B2 xml file</Description>
</A>
<A>
<Id>B-003</Id>
<Title1>B3 file</Title1>
<Description>This is B3 xml file</Description>
</A>
</Result>
merge.xslt file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:param name="fileName" select="'Result/*'" />
<xsl:param name="updates" select="document($fileName)" />
<xsl:variable name="updateItems" select="$updates/*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<h2>Merge Two XML Sheet By XSLT</h2>
<merge>
<xsl:copy>
<xsl:apply-templates select="Result/A"/>
<xsl:apply-templates select="$updateItems"/>
<table border="1">
<tr bgcolor="#9acd32">
<th>ID</th>
<th>Title</th>
<th>Description</th>
</tr>
<xsl:for-each select="Result/A">
<tr>
<td>
<xsl:value-of select="Id"/>
</td>
<td>
<xsl:value-of select="Title1"/>
</td>
<td>
<xsl:value-of select="Description"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:copy>
</merge>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
No comments :
Post a Comment