0

I have a got two XML variables and below are sample data in it:

$Variable1:

<Group>
    <A>Test</A>
    <B>Test1</B>
    .....
    .....
    .....
</Group>

$Variable2:

<Data>
    <ABC>Test</ABC>
    <XYZ>Test1</XYZ>
    .....
    .....
    .....
</Data>

Now I want to merge these two variable in XSLT and use the output in same XSLT, so output will be something like below after merge :

<Group>
    <A>Test</A>
    <B>Test1</B>
    .....
    .....
    .....

    <ABC>Test</ABC>
    <XYZ>Test1</XYZ>
    .....
    .....
    .....
</Group>

Above output will be passed in same XSLT for further processing.

Below is the xslt sample, I have tried:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="var1" select="document($Variable1)" />
  <xsl:param name="var2" select="document($Variable2)" />

  //Here I want to merge above to inputs and later will be used in XSLT below

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Group">
     -------
     -------
     -------
     -------
  </xsl:template>
</xsl:stylesheet>
Manoj Singh
  • 7,569
  • 34
  • 119
  • 198

1 Answers1

0

If you take the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="file1"/>
<xsl:param name="file2"/>

<xsl:template match="/">
    <xsl:apply-templates select="document($file1)/*"/>
</xsl:template>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:apply-templates select="document($file2)/*/*"/>
    </xsl:copy>
</xsl:template>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

and pass it the paths to your two files (as strings), will return the following result:

<?xml version="1.0" encoding="UTF-8"?>
<Group>
   <A>Test</A>
   <B>Test1</B>
   <ABC>Test</ABC>
   <XYZ>Test1</XYZ>
</Group>

Of course, you would also need a third (dummy) XML file on which to run the transformation. A smarter implementation would use the first input file as the source XML, and only pass the second file's path as the parameter.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Sorry for delay response.. ...in my case file1 and file2 are XML string which are generated in runtime...they are not a file which we can use for ..any suggestions – Manoj Singh Nov 20 '14 at 07:38
  • @ManojSingh There is no such thing as an "XML string". A string is a string - and there's not much you can do with a string using XSLT. See this recent question: http://stackoverflow.com/questions/27018244/apply-transforms-to-xml-attribute-containing-escaped-html/27019850#27019850 – michael.hor257k Nov 20 '14 at 08:37
  • Yes I know there is no such thing like XML String, I was just telling that file1 and file2 will having string values in it, which will be generated on runtime. – Manoj Singh Nov 20 '14 at 09:51