xsl-list
[Top] [All Lists]

Re: Merging two xml files

2004-01-13 08:10:48
Hi Arul,
  I have come up, probably with a simpler solution ;) 

<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="addresses"
select="document('employeeAddresses.xml')"/>
   <xsl:template match="/employees">
      <employees>
        <xsl:for-each select="employee">
           <xsl:variable name="id" select="@id"/>
           <xsl:variable name="pincode"
select="$addresses/employees/employee[$id =
@id]/@pincode"/>
          <employee pincode="{$pincode}">
             <lastname><xsl:value-of select="lastname"
/><
             </lastname>                                    
<firstname><xsl:value-of select="firstname" />
             </firstname>
             <address><xsl:value-of
select="$addresses/employees/employee/address" />
             </address>
          </employee>
    </xsl:for-each>
</employees>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

 --- Arulraj <p_arulraj(_at_)yahoo(_dot_)com> wrote: > 
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
 

________________________________________________________________________
Yahoo! India Mobile: Download the latest polyphonic ringtones.
Go to http://in.mobile.yahoo.com

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



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