Pages

Friday, September 6, 2013

Merging & then Sorting of 2 XML files through XSLT

*We have 2 xmls files 'xml1.xml' & 'xml2.xml', we have merge them & then sort by the year.

xml1.xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <news>
    <newstitle>All_September_a</newstitle>
    <date>2011-09-07T12:11:33.555</date>
    <newsby>Prateek</newsby>
    <description>Lorem ipsum dolor </description>
  </news>
  <news>
    <newstitle>All_July_a</newstitle>
    <date>2011-07-07T12:11:33.333</date>
    <newsby>Prateek</newsby>
    <description>c lectus. Duis ecu, ac tempor elit. Donec. </description>
  </news>
  <news>
    <newstitle>All_Dec_a</newstitle>
    <date>2011-12-07T11:11:33.566</date>
    <newsby>Ankit</newsby>
    <description>Lorem ipsum  testu, ac tempor elit. lor arcu, ac tempor elit. Donec. </description>
  </news>
</root>


xml2.xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <news>
    <newstitle>All_September_b</newstitle>
    <date>2011-09-07T12:11:33.555</date>
    <newsby>Prateek</newsby>
    <description>Lorem ipsum dolor </description>
  </news>
  <news>
    <newstitle>All_July_b</newstitle>
    <date>2011-07-07T12:11:33.333</date>
    <newsby>Prateek</newsby>
    <description>c lectus. Duis ecu, ac tempor elit. Donec. </description>
  </news>
  <news>
    <newstitle>All_Dec_b</newstitle>
    <date>2011-12-07T12:11:33.566</date>
    <newsby>Ankit</newsby>
    <description>Lorem ipsum  testu, ac tempor elit. lor arcu, ac tempor elit. Donec. </description>
  </news>
</root>



*Create an xml 'merge.xml', which works as a reference for both xmls which we want to merge & sort

mergeFiles.xml

<?xml version="1.0"?>
<list>
    <entry>xml1.xml</entry>
    <entry>xml2.xml</entry>
</list>



*Create Xslt file :

merge_sort_files.xslt 

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:param name="order" select="$order"></xsl:param>
  <xsl:template match="/*">
    <ul class="newslisting">
      <xsl:apply-templates
         select="document(entry)/root/news">
        <xsl:sort select="substring-before(substring-before(date,'T'),'-')" data-type="number" order="{$order}"></xsl:sort>
        <!--sort by year-->
      </xsl:apply-templates>
    </ul>
  </xsl:template>

  <xsl:template match="news">
    <li>
      <div class="nhead">
        <h3>
          <xsl:value-of select="newstitle"/>
        </h3>
      </div>
      <div class="npstby">
        <p>
          <span>Posted on</span>
          <xsl:value-of select="date"/>
        </p>
        <p>
          <span>by</span>
          <xsl:value-of select="newsby"/>
        </p>
      </div>
      <br />
      <div class="comment more">
        <xsl:value-of select="description"/>
      </div>
    </li>
  </xsl:template>
</xsl:stylesheet>


*Drag & Drop 'Xml' Control where we want to show  (program.aspx)

<div>
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
</div>

*Call the method & get Result : (program.aspx.cs)
  
 protected void Page_Load(object sender, EventArgs e)
{
TransformXsltWithArgs(Xml1, "mergeFiles.xml", "merge_sort_files.xslt", "descending");
}

*Method:

  private void TransformXsltWithArgs(Xml XmlControl, string xmlFile, string xsltFile, string order)
    {
        XsltArgumentList arg = new XsltArgumentList();
        arg.AddParam("order", "", order);
           XmlControl.TransformArgumentList = arg;
        XmlControl.DocumentSource = MapPath( xmlFile);
        XmlControl.TransformSource = MapPath(xsltFile);
    }



No comments:

Post a Comment