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

Re: Combining 2 XML into 1 XML via XSLT


xslt combine
The purpose is to generate a single xml file in which I can apply an xsl 
that we have that generates a matrix table comparrison of the two vehicles.

So its important that each vehicle have the same nodes whether empty or not. 
  In the example since the <header header_name="Misc"> node had the same row 
nodes in both xml's that was not merged.  If xml 2 has a header node that 
xml1 does not have then that would have to be merged as well.

Here is my xsl at this point.

<?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="/">
		<xsl:apply-templates select="vehicle" />
</xsl:template>
<xsl:template match="vehicle">
<xsl:variable name="xml2" 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>
		<image>
		<xsl:apply-templates select="image" mode="copy" />
		</image>
		<headers>
			<xsl:for-each select="$xml2//headers/header">
				<xsl:if test="@header_name=$xml2//@header_name">
					<xsl:variable name="bcheck">true</xsl:variable>
				</xsl:if>
				<xsl:if test="@bcheck='true'">
					<header>
						<xsl:attribute name="header_name"><xsl:value-of 
select="$xml2//@header_name"/></xsl:attribute>
					</header>
				</xsl:if>
			</xsl:for-each>
			<xsl:for-each select="//headers/header">
			<xsl:variable name="current_header" select="@header_name" />
				<header>
					<xsl:attribute name="header_name"><xsl:value-of 
select="@header_name"/></xsl:attribute>
					<xsl:for-each 
select="//headers/header[@header_name=($current_header)]/rows/row">
						<row>
							<xsl:attribute name="rows_name"><xsl:value-of select="@row_name" 
/></xsl:attribute>
							<xsl:value-of 
select="//headers/header[@header_name=($current_header)]/rows/row" />
						</row>
					</xsl:for-each>
				</header>
			</xsl:for-each>
		</headers>
	</vehicle>
	<vehicle>
		<xsl:attribute name="aic_id"><xsl:apply-templates  
select="document(xml2.xml)[@aic_id]" mode="copy"/></xsl:attribute>
		<xsl:copy-of select="$xml2//make" />
		<xsl:copy-of select="$xml2//year" />
		<xsl:copy-of select="$xml2//model" />
		<xsl:copy-of select="$xml2//style" />
		<xsl:copy-of select="$xml2//image" />
		<headers>
			<xsl:for-each select="$xml2//headers/header">
				<header>
					<xsl:attribute name="header_name"><xsl:apply-templates 
select="$xml2//@header_name"/></xsl:attribute>
				</header>
			</xsl:for-each>
			<xsl:for-each select="//headers/header">
				<header>
					<xsl:if test="$xml2//headers/header[@header_name!=@header_name]">
						<xsl:attribute name="header_name"><xsl:value-of 
select="@header_name"/></xsl:attribute>
					</xsl:if>
				</header>
			</xsl:for-each>
		</headers>
	</vehicle>
</vehicles>
</xsl:template>
</xsl:stylesheet>


----Original Message Follows----
From: Robin LaFontaine <robin.lafontaine@d...>

You seem to have in your output a <vehicle> for each of the input <vehicle> 
elements, but the <header name="Safety"> seem to have been merged in the two 
cases, whereas the <header name="Misc"> is not merged. This suggests you 
have very specific needs here.

One other small point: suggest you use xsl:strip-space to get rid of the 
spurious indentation (perhaps the files are not indented in originals).

Hope this helps a little.

Robin La Fontaine

[1] http://www.deltaxml.com

At 2:50 pm -0500 11/9/02, Dave Yancey wrote:
>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.
>

...cut

>-----------------------------------------------------------------
>The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
>initiative of OASIS <http://www.oasis-open.org>
>
>The list archives are at http://lists.xml.org/archives/xml-dev/
>
>To subscribe or unsubscribe from this list use the subscription
>manager: <http://lists.xml.org/ob/adm.pl>


--
-- -----------------------------------------------------------------
Robin La Fontaine, Director, Monsell EDM Ltd
DeltaXML: "Change control for XML, in XML"
Tel: +44 1684 592 144 Fax: +44 1684 594 504
Email: robin.lafontaine@d...      http://www.deltaxml.com

-----------------------------------------------------------------
The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
initiative of OASIS <http://www.oasis-open.org>

The list archives are at http://lists.xml.org/archives/xml-dev/

To subscribe or unsubscribe from this list use the subscription
manager: <http://lists.xml.org/ob/adm.pl>


----
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.