[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Combining 2 XML into 1 XML via XSLT

  • To: xml-dev@l...
  • Subject: Combining 2 XML into 1 XML via XSLT
  • From: "Dave Yancey" <dyancey1@h...>
  • Date: Wed, 11 Sep 2002 14:50:50 -0500
  • Bcc:

combine xml with xslt
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


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.