xsl-list
[Top] [All Lists]

Merging two xml files

2004-01-13 01:58:48

Hi,
  
I want to merge two xml files, I am using document()in
xsl file for accessing one file and another file given
in command prompt for the parser. 

first xml file given below
employeeNames.xml
<?xml version="1.0" encoding="UTF-8"?>
<employees>
        <employee id="1">
                <lastname>Smith</lastname>
                <firstname>Marsha</firstname>
        </employee>
        <employee id="2">
                <lastname>Arul</lastname>
                <firstname>Raj</firstname>
        </employee>
        <employee id="3">
                <lastname>Jaya</lastname>
                <firstname>Raj</firstname>
        </employee>
</employees>

second xml file
employeeAddresses.xml

<?xml version="1.0" encoding="UTF-8"?>
<employees>
        <employee id="1" pincode="4">
                <address>115 Marshal Rd., 
      Greenville, MN 39281</address>
        </employee>
        <employee id="2" pincode="34">
                <address>chennai,India</address>
        </employee>
        <employee id="3" pincode="47">
                <address>Chennai</address>
        </employee>
</employees>

I am merging this XML file using XSLT
given below

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="xml" indent="yes"/>

<!-- load the merge file -->
<xsl:variable name="emps"
select="document('employeeAddresses.xml')"/>

<xsl:template match="/">

   <employees>
   <xsl:for-each select="employees/employee">

   <!-- cache the employee's ID -->
   <xsl:variable name="id">
      <xsl:value-of select="@id"/></xsl:variable>

      <!-- copy the child nodes -->
      <employee>
      <xsl:copy-of select="child::*"/>

         <!-- copy the children of the matching
employee node 
         from the merge file -->
         <xsl:copy-of
select="$emps/employees/employee[(_at_)id=$id]/child::*" />
      </employee>
   </xsl:for-each>
   </employees>
</xsl:template>
</xsl:stylesheet>

Expected Output is:

<employees>
<employee pincode="4">
<lastname>Smith</lastname>
<firstname>Marsha</firstname>
<address>115 Marshal Rd., 
      Greenville, MN 39281</address>
</employee>
<employee pincode="34">
<lastname>Arul</lastname>
<firstname>Raj</firstname>
<address>chennai,India</address>
</employee>
<employee pincode="47">
<lastname>Jaya</lastname>
<firstname>Raj</firstname>
<address>Chennai</address>
</employee>
</employees>

By using above XSL I want get pincode attribute in
output file.
How to check that attribute exists in <employee> and
insert pincode attribute in produced output?

Please reply if you know,
Thanks in advance
Arul

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>