p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Combine multiple xml files to create one xml file (http://p2p.wrox.com/showthread.php?t=87096)

jonnyuk3 March 28th, 2012 10:53 AM

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

samjudson March 28th, 2012 11:26 AM

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]"/>


jonnyuk3 March 29th, 2012 11:19 AM

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

samjudson March 29th, 2012 11:26 AM

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.

jonnyuk3 March 29th, 2012 11:35 AM

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

mhkay March 29th, 2012 11:44 AM

>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!)

jonnyuk3 March 29th, 2012 11:51 AM

fair enough.


All times are GMT -4. The time now is 10:43 AM.

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