OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   [xml-dev] Combining 2 XML into 1 XML via XSLT

[ Lists Home | Date Index | Thread Index ]

I'm trying to combine two xml files into a single xml file via xslt.  I know 
how to do it using copy-of but certain requirments must be met.

The two xml files are vehicle features. And are similiar.

In the xml there are header nodes and row nodes.

xml1 may contain certain header nodes and row nodes that xml2 doesn't.

In the resulting xml each vehicle must contain the same header and row nodes 
with the correct values (empty or their existing values).

Here is an example of each xml file and what the result is supposed to look 
like.

--------------xml1 -----------------

<vehicle aic_id="1388">
	<make>BMW</make>
	<year>2002</year>
	<model>325i</model>
	<style>Sedan</style>
	<image>1388UI.GIF</image>
	<headers>
		<header name="Misc">
			<rows>
				<row name="MSRP Base Price">$20,000</row>
				<row name="Cup Holder">2" diameter</row>
			</rows>
		</header>
		<header name="Safety">
			<rows>
				<row name="Seatbelt">Standard</row>
				<row name="ABS">Optional</row>
				<row name="Wipers">Need replace</row>
			</rows>
		</header>
	</headers>
</vehicle>

-------------------xml 2-------------------------------
<vehicle aic_id="3282">
    <make>BMW</make>
    <year>2002</year>
    <model>330i</model>
    <style>Convertible</style>
    <image>3282UI.GIF</image>
    <headers>
         <header name="Misc">
              <rows>
                   <row name="MSRP Base Price">$10,000</row>
                   <row name="Cup Holder">2" diameter</row>
              </rows>
         </header>
         <header name="Safety">
              <rows>
                   <row name="Tires">Standard</row>
                   <row name="Breaks">Optional</row>
                   <row name="Wipers">Need replace</row>
              </rows>
         </header>
    </headers>
</vehicle>

------------resulting xml -------------------------------

<vehicles>
<vehicle id=1388>
   <make>BMW</make>
   <year>2002</year>
   <model>325i</model>
   <style>Sedan</style>
   <image>1388UI.GIF</image>
   <headers>
        <header name="Misc">
             <rows>
                  <row name="MSRP Base Price">$10,000</row>
                  <row name="Cup Holder">2" diameter</row>
             </rows>
        </header>
        <header name="Safety">
             <rows>
                  <row name="Tires" />
                  <row name="Breaks" />
                  <row name="Seatbelt">Standard</row>
                  <row name="ABS">Optional</row>
                  <row name="Wipers">Need replace</row>
             </rows>
        </header>
   </headers>
</vehicle>
<vehicle id=1388>
   <make>BMW</make>
   <year>2002</year>
   <model>330i</model>
   <style>Convertible</style>
   <image>1388UI.GIF</image>
   <headers>
        <header name="Misc">
             <rows>
                  <row name="MSRP Base Price">$50,000</row>
                  <row name="Cup Holder"></row>
             </rows>
        </header>
        <header name="Safety">
             <rows>
                  <row name="Tires">Standard</row>
                  <row name="Breaks">Optional</row>
                  <row name="Seatbelt" />
                  <row name="ABS" />
                  <row name="Wipers">Need replace</row>
             </rows>
        </header>
   </headers>
</vehicle>
</vehicles>


Here is my xslt I have thus far.

------------- xslt ---------------------------

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="vehicle">
<xsl:variable name="index" select="document(xml2.xml)" />
<vehicles>
	<vehicle>
		<xsl:attribute name="aic_id"><xsl:value-of select="@aic_id" 
/></xsl:attribute>
		<make>
		<xsl:apply-templates select="make" mode="copy" />
		</make>
		<year>
		<xsl:apply-templates select="year" mode="copy" />
		</year>
		<model>
		<xsl:apply-templates select="model" mode="copy" />
		</model>
		<style>
		<xsl:apply-templates select="style" mode="copy" />
		</style>
		<headers>
			<xsl:for-each select="$index/headers/header">
				<header>
					<xsl:if test="headers/header[@name!=$index/headers/header[@name]]">
						<xsl:attribute name="name"><xsl:value-of 
select="$index/headers/header[@name]"/></xsl:attribute>
					</xsl:if>
				</header>
			</xsl:for-each>
			<xsl:for-each select="headers/header">
				<header>
					<xsl:attribute name="name"><xsl:apply-templates 
select="headers/header[@name]"/></xsl:attribute>
				</header>
			</xsl:for-each>
		</headers>
	</vehicle>
	<vehicle>
		<xsl:attribute name="aic_id"><xsl:apply-templates  
select="$index[@aic_id]" mode="copy"/></xsl:attribute>
		<make>
			<xsl:apply-templates select="$index/make" mode="copy" />
		</make>
		<year>
			<xsl:apply-templates select="$index/year" mode="copy" />
		</year>
		<model>
			<xsl:apply-templates select="$index/model" mode="copy" />
		</model>
		<style>
			<xsl:apply-templates select="$index/style" mode="copy" />
		</style>
		<headers>
			<xsl:for-each select="$index/headers/header">
				<header>
					<xsl:attribute name="name"><xsl:apply-templates 
select="$index/headers/header[@name]"/></xsl:attribute>
				</header>
			</xsl:for-each>
			<xsl:for-each select="headers/header">
				<header>
					<xsl:if test="$index/headers/header[@name!=headers/header[@name]]">
						<xsl:attribute name="name"><xsl:value-of 
select="headers/header[@name]"/></xsl:attribute>
					</xsl:if>
				</header>
			</xsl:for-each>
		</headers>
	</vehicle>
</vehicles>
</xsl:template>
</xsl:stylesheet>


Its close but not quite giving me the results I need.

First the headers are empty and the second vehicle each element is empty as 
well.  (I don't have the row's in the xsl yet cause I've been working on the 
headers.)

Any help is much appreciated.

David Yancey

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS