Merging XML Files of a folder

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Merging XML Files of a folder

Vincent Cestmois
Hi,

I'd need an xlst file to merge a bunch of xml files in a folder

each file has the following structure

<?xml version="1.0" encoding="UTF-8"?>
<!-- source: stuff 2016 -->
<Stuff>
<Product>
....  many nodes
</Product>
</Stuff>

each file only has one product

I'd like

<?xml version="1.0" encoding="UTF-8"?>
<!-- source: stuff 2016 -->
<Stuff>
<Product>
</Product>
<Product>
</Product>
<Product>
</Product>
</Stuff>

Preserving the headers

I found something about collection but can't make it work
I'm on mac os x, using saxon EE

Thanks for your help


------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Martin Honnen-2
On 26.11.2016 11:43, Vincent Cestmois wrote:

> I'd need an xlst file to merge a bunch of xml files in a folder
>
> each file has the following structure
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE Stuff "http://data.icecat.biz/dtd/ICECAT-interface_response.dtd">
> <!-- source: stuff 2016 -->
> <Stuff>
> <Product>
> ....  many nodes
> </Product>
> </Stuff>
>
> each file only has one product
>
> I'd like
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE Stuff "http://data.icecat.biz/dtd/ICECAT-interface_response.dtd">
> <!-- source: stuff 2016 -->
> <Stuff>
> <Product>
> </Product>
> <Product>
> </Product>
> <Product>
> </Product>
> </Stuff>
>
> Preserving the headers
>
> I found something about collection but can't make it work
> I'm on mac os x, using saxon EE


Use a stylesheet that starts with named template (call Saxon with 'it:main')

<xsl:template name="main">
   <Stuff>
     <xsl:copy-of
select="collection('file:///dir/subdir?select=*.xml')//Product"/>
   <Stuff>
</xsl:template>

and then outputs the doctype using

   <xsl:output method="xml"
               indent="yes"
 
doctype-system="http://data.icecat.biz/dtd/ICECAT-interface_response.dtd"/>

There is really no way with pure XSLT to copy the doctype from the input
so you need to put the xsl:output in there.

------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Vincent Cestmois
In reply to this post by Vincent Cestmois
Thanks Martin,

so I created and newmerge.xslt file whose contents his :

<xsl:template name="main">
   <Stuff>
     <xsl:copy-of 
select="collection('file:///Volumes/FMPserver/Users/forvince/Desktop/Archive?select=*.xml')//Product"/>;
   <Stuff>
</xsl:template>

   <xsl:output method="xml" indent="yes" doctype-system="http://data.icecat.biz/dtd/ICECAT-interface_response.dtd"/>;


and then I launched the following command (from the Archive folder that contains the xml files and the xlst)

/usr/bin/java -jar /Volumes/FMPServer/Applications/SaxonEE9-7-0-8J/saxon9ee.jar -it:main  491244.xml newmerge.xslt -o:merged.xml


But I get an error :

Unrecognized option: newmerge.xslt


Also, I think that my path in the collection is maybe wrong, as the example you gave seems to be windows style path

And I would prefer a way to not specify the path but have the xlst use the folder it resides in.


Thanks for you help and sorry for my complete noobines

Best regards,

Vincent



------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Martin Honnen-2
On 27.11.2016 13:38, Vincent Cestmois wrote:

> so I created and newmerge.xslt file whose contents his :

For a complete stylesheet you need to wrap the parts I posted and you
quoted below into

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

>
> <xsl:template name="main">
>    <Stuff>
>      <xsl:copy-of
> select="collection('file:///Volumes/FMPserver/Users/forvince/Desktop/Archive?select=*.xml')//Product"/>;
>    <Stuff>
> </xsl:template>
>
>    <xsl:output method="xml" indent="yes"
> doctype-system="http://data.icecat.biz/dtd/ICECAT-interface_response.dtd"/>;

</xsl:stylesheet>

> and then I launched the following command (from the Archive folder that
> contains the xml files and the xlst)
>
> /usr/bin/java -jar
> /Volumes/FMPServer/Applications/SaxonEE9-7-0-8J/saxon9ee.jar -it:main
> 491244.xml newmerge.xslt -o:merged.xml

Don't provide an input file (as you start with that named template) and use

/usr/bin/java -jar
/Volumes/FMPServer/Applications/SaxonEE9-7-0-8J/saxon9ee.jar -it:main
-xsl:newmerge.xslt -o:merged.xml


> Also, I think that my path in the collection is maybe wrong, as the
> example you gave seems to be windows style path

I used a file URI, admittedly I can't tell you exactly how that looks on
a Mac but I think the sample you have should be fine.

> And I would prefer a way to not specify the path but have the xlst use
> the folder it resides in.

I think in that case

   <xsl:copy-of
select="collection('.?select=*.xml')//Product"/>

will do (incorporated into a full stylesheet as indicated above).






------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Vincent Cestmois
In reply to this post by Vincent Cestmois
Thanks Martin,

The following worked, but there's a problem, and that's my fault because my description of the nodes  was lacking

see, there's is also <Product> subnodes

so the exact structure

 <ICECAT-interface>
<Product>
      <supplier></supplier>
     <related_products>
       <Product></Product>
      <Product></Product>
      <Product></Product>
    </related_products>
</Product>
</ICECAT-interface>

Unfortunately, it seems that "collection('.?select=*.xml')//Product", fetches the sub product nodes and puts them up in the tree

is there a way to restrict the reach of the collection instruction ?

I tried "collection('.?select=*.xml')" but then I got

 <ICECAT-interface>
<Product>
</Product>
</ICECAT-interface>
 <ICECAT-interface>
<Product>
</Product>
</ICECAT-interface>

whereas I need

 <ICECAT-interface>
<Product></Product>
<Product></Product>
</ICECAT-interface>


here's my xlst

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform
version="2.0">


<xsl:template name="main">
   <ICECAT-interface>
   <xsl:copy-of
select="collection('.?select=*.xml')//Product"/>
   </ICECAT-interface>
</xsl:template>

<xsl:output method="xml" indent="yes" doctype-system="http://data.icecat.biz/dtd/ICECAT-interface_response.dtd"/>


</xsl:stylesheet>

------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Martin Honnen-2
On 27.11.2016 17:49, Vincent Cestmois wrote:

> Thanks Martin,
>
> The following worked, but there's a problem, and that's my fault because
> my description of the nodes  was lacking
>
> see, there's is also <Product> subnodes
>
> so the exact structure
>
>  <ICECAT-interface>
> <Product>
>       <supplier></supplier>
>      <related_products>
>        <Product></Product>
>       <Product></Product>
>       <Product></Product>
>     </related_products>
> </Product>
> </ICECAT-interface>
>
> Unfortunately, it seems that "collection('.?select=*.xml')//Product",
> fetches the sub product nodes and puts them up in the tree


It is simply a matter of using a proper XPath expression so

      <xsl:copy-of
select="collection('.?select=*.xml')/ICECAT-interface/Product"/>

will copy the Product child elements of the ICECAT-interface root element.

Try an XPath tutorial to learn to write more complicated paths if needed.


------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Merging XML Files of a folder

Vincent Cestmois
In reply to this post by Vincent Cestmois
Terrific Martin,
It worked. My problem is solved.
Thank you so much for your help and patience !
You saved my bacon

------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help