1

I have two XML Files that looks like:

...........
XML File 1:
...........
<Result>
  <Id>111</Id> 
  <Title>Result 111 title</Title>
  <Description>Result 111 Description</Description>
</Result>

...........
XML File 2:
...........

<Result>
  <Id>222</Id> 
  <Title>Result 222 title</Title>
  <Description>Result 222 Description</Description>
</Result>

I have XSLT that produces a design like this:

|ID |
|Title :| |Result 111 Title|
|Description:| |Result 111 Description|     

What i want is i also want to add the elements value from 2nd XML File so the design will look like this:

|ID |
|Title :| |Result 111 Title|
|Description:| |Result 111 Description|

|ID |
|Title :| |Result 222 Title|
|Description:| |Result 222 Description|    

This design will be produced during the run time in C#. I have so far applied one XML to One XSLT. But this is different. How can i achieve this. Please treat "||" to be a Design of "" tag. Any help really appreciated. Thanks..! :)

Bravo11
  • 898
  • 2
  • 11
  • 24
  • possible duplicate of [how to Merge two xml files with XSLT](http://stackoverflow.com/questions/15194718/how-to-merge-two-xml-files-with-xslt) – rene Jul 08 '13 at 08:32
  • In C# don't forget to call load with the overload to provide the XsltSetting with [EnableDocument](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltsettings.enabledocumentfunction.aspx) = true; – rene Jul 08 '13 at 08:35
  • there are lots of posts about merging but none talks about using C# and run time which is a lot different – Bravo11 Jul 08 '13 at 08:48

1 Answers1

0

Something like this (inspired by this answer):

var xslt = new XslCompiledTransform();
xslt.Load("merge.xslt", new XsltSettings{EnableDocumentFunction = true}, null );

var xmlr = XmlReader.Create("first.xml");
var xmlw = XmlWriter.Create("result.html");
var xslargs = new XsltArgumentList();
xslargs.AddParam("fileName", "", "second.xml");
xslt.Transform(xmlr, xslargs , xmlw);, xmlw);

and the xslt:

  <xsl:output method="html" indent="yes"/>

  <xsl:param name="fileName" select="'somefile'" />
  <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="/">
    <merge>
    <xsl:copy>
      <xsl:apply-templates select="*" />
      <xsl:apply-templates select="$updateItems" />
    </xsl:copy>
    </merge>
  </xsl:template>

On second thought this can be achieved even easier, where you can add an arbritrary number of files to be merged.

Code

var xslt = new XslCompiledTransform();
xslt.Load("merge.xslt", new XsltSettings{EnableDocumentFunction = true}, null );

using (var xmlr = XmlReader.Create(
    new StringReader(@"<files><file>first.xml</file><file>second.xml</file></files>")))
{
    using (var xmlw = XmlWriter.Create("result.html"))
    { 
        xslt.Transform(xmlr, null , xmlw);
    }
}

Xslt

<xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="/files/file">
          <xsl:apply-templates select="document(.)/*" />
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

References:
Load
Transform

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152