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..! :)

3
  • possible duplicate of how to Merge two xml files with XSLT Commented Jul 8, 2013 at 8:32
  • In C# don't forget to call load with the overload to provide the XsltSetting with EnableDocument = true; Commented Jul 8, 2013 at 8:35
  • there are lots of posts about merging but none talks about using C# and run time which is a lot different Commented Jul 8, 2013 at 8:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.