Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 28th, 2012, 10:53 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Thumbs up Combine multiple xml files to create one xml file

Hi all,

I currently have 3 xml files and would like to transform these into one xml file if that is at all possible.

first xml file
HTML Code:
<?xml version="1.0"?>
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Products>
    <Product>
      <Id>5024</Id>
      <Sku>S2</Sku>
      <Barcode />
      <Name>S2</Name>
      <Description />
      <Status>0</Status>
      <SalePrice>0</SalePrice>
      <QtyInStock>0</QtyInStock>
      <Attributes>
        <Attribute>
          <Name>Current Stock</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
        <Attribute>
          <Name>Amber Stock</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
        <Attribute>
          <Name>Warehouse Name</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
      </Attributes>
    </Product>
  </Products>
</Company>
second xml file
HTML Code:
<?xml version="1.0"?>
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Products />
  <Styles />
  <Inventories>
    <Inventory>
      <Id xsi:nil="true" />
      <Sku>S2</Sku>
      <Barcode />
      <Locations>
        <Location>
          <Name>01. General W/H</Name>
          <QtyInStock>0</QtyInStock>
          <QtyOnOrder>0</QtyOnOrder>
          <QtyAllocated>0</QtyAllocated>
          <AllowSalesTrading>true</AllowSalesTrading>
        </Location>
      </Locations>
    </Inventory>
  </Inventories>
</Company>
third xml file
HTML Code:
<?xml version="1.0" standalone="yes"?>
<Root>
  <ProductGroup>
    <ID>5024</ID>
    <Sku>S2</Sku>
    <ProductGroupID>4605</ProductGroupID>
    <ProductGroupCode>NONS</ProductGroupCode>
    <Description>Non Stock Item</Description>
  </ProductGroup>
</Root>
and finally, what i would like
HTML Code:
<?xml version="1.0"?>
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Products>
    <Product>
      <Id>5024</Id>
      <Sku>S2</Sku>
      <Barcode />
      <Name>S2</Name>
      <Description />
      <Status>0</Status>
      <SalePrice>0</SalePrice>
      <QtyInStock>0</QtyInStock>
      <Attributes>
        <Attribute>
          <Name>Current Stock</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
        <Attribute>
          <Name>Amber Stock</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
        <Attribute>
          <Name>Warehouse Name</Name>
          <Value>&lt;NONE&gt;</Value>
        </Attribute>
      </Attributes>
      <Locations>
        <Location>
          <Name>01. General W/H</Name>
          <QtyInStock>0</QtyInStock>
          <QtyOnOrder>0</QtyOnOrder>
          <QtyAllocated>0</QtyAllocated>
          <AllowSalesTrading>true</AllowSalesTrading>
        </Location>
      </Locations>
      <ProductGroup>
          <ProductGroupCode>NONS</ProductGroupCode>
          <Description>Non Stock Item</Description>
      </ProductGroup>
    </Product>
  </Products>
</Company>
How would I go about doing this, i've looked around on the internet for some solutions but i'm still stuck!!

Any help would be much appreciated.

Thanks,

jonnyuk3
  #2 (permalink)  
Old March 28th, 2012, 11:26 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well you can load another XML document using the document() function.

http://www.w3schools.com/xsl/func_document.asp

Often when using multiple files you need to keep a reference to your original document handy as well.
Store the result in a variable, and the refer to it using normal XPath:

e.g.

Code:
<xsl:variable name="original" select="/"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:apply-templates select="$file2//ProductGroup[Id=$original//Product/Id]"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
jonnyuk3 (March 29th, 2012)
  #3 (permalink)  
Old March 29th, 2012, 11:19 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default

thanks for the reply.

I am able to output the information I want to a csv file but want to output this to xml, how would i go about that?

Here's what i currently have:

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
	
<xsl:variable name="product_group" select="document('product_group.xml')/ProductGroups"/>
<xsl:variable name="stock_inventory" select="document('stock_inventory.xml')/Company"/>
	
<xsl:template match="/">
    
	<xsl:text>"ProductGroupCode"</xsl:text>
	<xsl:text>,</xsl:text>
	<xsl:text>"ProductGroupDesc"</xsl:text>
	<xsl:text>,</xsl:text>
	<xsl:text>"SKU"</xsl:text>
	<xsl:text>,</xsl:text>
	<xsl:text>"Name"</xsl:text>
	<xsl:text>,</xsl:text>
	<xsl:text>"QtyInStock"</xsl:text>
	<xsl:text>,</xsl:text>
	
	<xsl:for-each select="Company/Products/Product[Attributes/Attribute[Name='Current Stock' and Value='Yes']]">
		<xsl:sort select="Sku"/>
		
		<xsl:variable name="original_sku" select="Sku"></xsl:variable>
		
		<!-- Product group code -->
		<xsl:text>"</xsl:text>
		<xsl:value-of select="$product_group/ProductGroup[Sku=$original_sku]/ProductGroupCode"/>
		<xsl:text>"</xsl:text>
		<xsl:text>,</xsl:text>
		<!-- Product group description -->
		<xsl:text>"</xsl:text>
		<xsl:value-of select="$product_group/ProductGroup[Sku=$original_sku]/Description"/>
		<xsl:text>"</xsl:text>
		<xsl:text>,</xsl:text>
		<!-- output sku -->
		<xsl:text>"</xsl:text>
		<xsl:value-of select="Sku"/>
		<xsl:text>"</xsl:text>
		<xsl:text>,</xsl:text>
		<!-- output name -->
		<xsl:text>"</xsl:text>
		<xsl:value-of select="Name"/>
		<xsl:text>"</xsl:text>
		<xsl:text>,</xsl:text>
		<!-- qty in stock -->
		<xsl:text>"</xsl:text>
		<xsl:value-of select="$stock_inventory/Inventories/Inventory[Sku=$original_sku]/Locations/Location/QtyInStock"/>
		<xsl:text>"</xsl:text>
		<xsl:text>,</xsl:text>
		
                <!--add the newline character at the end-->
		<xsl:text>&#xa;</xsl:text>		
	</xsl:for-each>
	
</xsl:template> 
</xsl:stylesheet>
I am sure this isn't too far away, I just need another nudge in the right direction.

Thanks

jonnyuk3
  #4 (permalink)  
Old March 29th, 2012, 11:26 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Err, well you'd get it to output XML rather than CSV by writing XML in the XSLT, rather than text.

e.g.

Code:
<Sku><xsl:value-of select="Sku"/></Sku>
or indeed if you want to copy an element directly then just:

Code:
<xsl:copy select="Sku"/>
I find it hard to believe that you have managed to learn to get XSLT to output csv, something which is not its primary aim, and don't know how to output XML, when that is its usual purpose.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
jonnyuk3 (March 29th, 2012)
  #5 (permalink)  
Old March 29th, 2012, 11:35 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks samjudson.

This is my first foray in to XML and XSLT, i'm just learning the bits I need as I go along.
Thanks for your help, its much appreciated.

jonnyuk3
  #6 (permalink)  
Old March 29th, 2012, 11:44 AM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 Posts
Default

>i'm just learning the bits I need as I go along

It's a learning style we're all forced into occasionally - I had to modify some Ruby scripts recently, and it was all done by trial and error. But if you have to do more than change a line or two of someone else's code, then it's a very inefficient way of learning, and asking a forum for help whenever you need a new bit of information is also a great waste of everyone else's time. If you want to write XSLT, get a good book and spend the weekend reading it before you write any more code. (This is a Wrox forum, after all!)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
  #7 (permalink)  
Old March 29th, 2012, 11:51 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default

fair enough.
Closed Thread


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent ::Reading two xml files subcontent and adding that to third xml file archleader XSLT 6 August 18th, 2009 03:11 AM
How to combine stuff from two XML-files? Lemminkäinen XSLT 4 June 15th, 2006 08:03 AM
grouping multiple xml files in one file bcogney XSLT 3 April 21st, 2006 03:58 AM
Problem to create an xml file from two xml files saurabh_inblore XSLT 1 April 12th, 2006 02:58 AM



All times are GMT -4. The time now is 07:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.