EDN Admin
Well-known member
Hi,
I am trying to merge 3 XML docs, and I am getting slightly confused on how to do this. In my previous conversations in the forum I was able to achieve this. Here is what I have tried so far.
Say I have the following XML saved as an XML file itself locally.
XML1:
<Employees>
<Employee>
<ID>1</ID>
</Employee>
<Employee>
<ID>2</ID>
</Employee>
</Employees>
XML2:
<Employees>
<EmpData>
<FName>1</FName>
</EmpData>
<EmpData>
<FName>2</FName>
</EmpData>
</Employees>
XML3:
<Employees>
<EmpInfo>
<FNew>1</FNew>
</EmpInfo>
<EmpInfo>
<FNew>2</FNew>
</EmpInfo>
</Employees>
Now I am trying to merge all this Like:
<Employees>
<TestXML>
<TestName>TN</TestName>
</TestXML>
<Employee>
<ID>1</ID>
</Employee>
<Employee>
<ID>2</ID>
</Employee>
<EmpData>
<FName>1</FName>
</EmpData>
<EmpData>
<FName>2</FName>
</EmpData>
<EmpInfo>
<FNew>1</FNew>
</EmpInfo>
<EmpInfo>
<FNew>2</FNew>
</EmpInfo>
</Employees>
In this the first set of data should be some hard coded elements and rest are all from XML1, 2 & 3.
Since all the 3 files are stored locally in my machine, I am trying to pass this as an XML Document from the C#Code.
Like:
StringBuilder sb = new StringBuilder();<br/>
TextWriter tw = new StringWriter(sb);<br/>
<br/>
XmlDocument docA = new XmlDocument();<br/>
docA.Load(xml1);
XmlDocument docB = new XmlDocument();<br/>
docB.Load(xml2);<br/>
XmlDocument docC = new XmlDocument();<br/>
docC.Load(xml3); <br/>
XsltArgumentList xsltArgs = new XsltArgumentList();<br/>
<br/>
xsltArgs.AddParam("doc2", "", docB);
xsltArgs.AddParam("doc3","",docC);
proc.Transform(docA, xsltArgs, tw); <br/>
return sb.ToString();
and in the XSL I had:
<?xml version="1.0" encoding="utf-8"?><br/>
<xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/XSL/Transform <br/>
<xslutput method="xml" indent="yes"/><br/>
<xslaram name="doc2" select="/.."/><br/>
<xslaram name="doc3" select="/.."/>
<xsl:template match="@* | node() <br/>
<xsl:copy><br/>
<xsl:apply-templates select="@* | node()"/><br/>
</xsl:copy><br/>
</xsl:template><br/>
<br/>
<xsl:template match="Employees <br/>
<xsl:copy><br/>
<xsl:apply-templates select="@* | node()"/><br/>
<xsl:apply-templates select="$doc2"/><br/>
<xsl:apply-templates select="$doc3"/><br/>
</xsl:copy><br/>
</xsl:template>
The files that I choose will differ every time for each of the XML. I am not sure where I am going wrong or doing something different in the XSL. Is there a rule in the way I pass the parameter to XSL which is an XML file. Please advice. Thanks.
<hr class="sig Rpaul
View the full article
I am trying to merge 3 XML docs, and I am getting slightly confused on how to do this. In my previous conversations in the forum I was able to achieve this. Here is what I have tried so far.
Say I have the following XML saved as an XML file itself locally.
XML1:
<Employees>
<Employee>
<ID>1</ID>
</Employee>
<Employee>
<ID>2</ID>
</Employee>
</Employees>
XML2:
<Employees>
<EmpData>
<FName>1</FName>
</EmpData>
<EmpData>
<FName>2</FName>
</EmpData>
</Employees>
XML3:
<Employees>
<EmpInfo>
<FNew>1</FNew>
</EmpInfo>
<EmpInfo>
<FNew>2</FNew>
</EmpInfo>
</Employees>
Now I am trying to merge all this Like:
<Employees>
<TestXML>
<TestName>TN</TestName>
</TestXML>
<Employee>
<ID>1</ID>
</Employee>
<Employee>
<ID>2</ID>
</Employee>
<EmpData>
<FName>1</FName>
</EmpData>
<EmpData>
<FName>2</FName>
</EmpData>
<EmpInfo>
<FNew>1</FNew>
</EmpInfo>
<EmpInfo>
<FNew>2</FNew>
</EmpInfo>
</Employees>
In this the first set of data should be some hard coded elements and rest are all from XML1, 2 & 3.
Since all the 3 files are stored locally in my machine, I am trying to pass this as an XML Document from the C#Code.
Like:
StringBuilder sb = new StringBuilder();<br/>
TextWriter tw = new StringWriter(sb);<br/>
<br/>
XmlDocument docA = new XmlDocument();<br/>
docA.Load(xml1);
XmlDocument docB = new XmlDocument();<br/>
docB.Load(xml2);<br/>
XmlDocument docC = new XmlDocument();<br/>
docC.Load(xml3); <br/>
XsltArgumentList xsltArgs = new XsltArgumentList();<br/>
<br/>
xsltArgs.AddParam("doc2", "", docB);
xsltArgs.AddParam("doc3","",docC);
proc.Transform(docA, xsltArgs, tw); <br/>
return sb.ToString();
and in the XSL I had:
<?xml version="1.0" encoding="utf-8"?><br/>
<xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/XSL/Transform <br/>
<xslutput method="xml" indent="yes"/><br/>
<xslaram name="doc2" select="/.."/><br/>
<xslaram name="doc3" select="/.."/>
<xsl:template match="@* | node() <br/>
<xsl:copy><br/>
<xsl:apply-templates select="@* | node()"/><br/>
</xsl:copy><br/>
</xsl:template><br/>
<br/>
<xsl:template match="Employees <br/>
<xsl:copy><br/>
<xsl:apply-templates select="@* | node()"/><br/>
<xsl:apply-templates select="$doc2"/><br/>
<xsl:apply-templates select="$doc3"/><br/>
</xsl:copy><br/>
</xsl:template>
The files that I choose will differ every time for each of the XML. I am not sure where I am going wrong or doing something different in the XSL. Is there a rule in the way I pass the parameter to XSL which is an XML file. Please advice. Thanks.
<hr class="sig Rpaul
View the full article