Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combining xml files

Status
Not open for further replies.

bottom

Technical User
May 3, 2004
1
IE
I have three xml files.
They are unrelated and have no common tags.
I want to construct a single xml file that will output the contents of the other three.

Is this possible?
I have just started this, so any examples would be great.

Here are two of the files I want to combine

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="patient.xsl" type="text/xsl"?>
<!DOCTYPE PatientDetails SYSTEM "patient.dtd">

<PatientDetails name="HAEMATOLOGY DEPT. MATER HOSP. DUBLIN 7">
<Patient>
<Name>
<LastName>Smith</LastName>
<FirstName>Claire</FirstName>
</Name>
<Address>
<Street>13 My Street</Street>
<Town>AnyTown</Town>
<City>Dublin</City>
<Country>Ireland</Country>
</Address>
<DOB>
<Day>20 </Day>
<Month>06 </Month>
<Year>1981</Year>
</DOB>
<Sex>Female</Sex>
<Nation>Irish</Nation>
</Patient>
</PatientDetails>

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="Clinician.xsl" type="text/xsl"?>
<!DOCTYPE ClinicianDetails SYSTEM "Clinician.dtd">

<ClinicianDetails name="HAEMATOLOGY DEPT. MATER HOSP. DUBLIN 7">
<Clinician>
<ClinicianName>
<LastName>Bates</LastName>
<FirstName>Mel</FirstName>
</ClinicianName>
<IDNo>567</IDNo>
<PagerNo>0000</PagerNo>
<PhoneNo>086231431</PhoneNo>
<JobDescription>Intern</JobDescription>
</Clinician>
</ClinicianDetails>
 
You can do this. The technique is to use the "document" function. Here's a simple example using two xml files called a.xml and b.xml and a stylesheet that combines them called ab.xsl

a.xml's contents
Code:
<my_a>
    <data q="3" />
    <data q="4" />
    <data q="5" />
</my_a>

b.xml's contents
Code:
<my_b>
    <cool q="3" ret="g"/>
    <cool q="4" ret="h"/>
    <cool q="5" ret="i"/>
</my_b>
and here's ab.xsl:
Code:
<xsl:stylesheet
   xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
   version="1.0">


   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="data">
        <nd>
            <xsl:attribute name="mydata">
                <xsl:value-of select="@q" />
            </xsl:attribute>
            <xsl:attribute name="myret">
                <xsl:variable name="mykey" select="@q"/>
                <xsl:value-of select="document('b.xml')/my_b/cool[@q=$mykey]/@ret" />
            </xsl:attribute>
        </nd>
   </xsl:template>

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

</xsl:stylesheet>

The output of ab.xsl after transformation is:
Code:
<?xml version="1.0" encoding="utf-8"?>
<my_a>

   <nd mydata="3" myret="g"/>

   <nd mydata="4" myret="h"/>

   <nd mydata="5" myret="i"/>

</my_a>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top