Can saxon merge severa xml files, and how

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can saxon merge severa xml files, and how

Vincent Cestmois
Hi,

I'd like to merge several files that are like this
<xml>
  <catalog> 
    <product>
      <size>
       <length>52</length>
       <width>52</width>
      </size>
      <name>My product</name>
    </product>
  </catalog>
</xml>

to get a file like that 

<xml>
  <catalog> 
    <product>
      <size>
       <length>52</length>
       <width>52</width>
      </size>
      <name>My product</name>
    </product>
<product>
      <size>
       <length>25</length>
       <width>32</width>
      </size>
      <name>My product B</name>
    </product>
  </catalog>
</xml>

I've seen on stack overflow I could create a xslt for this, but as I need to merge hundreds of file, I would need a way to pass all the filenames at one, rather than having to issue hundreds of transforms

Thanks

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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Martin Honnen-2
On 07.09.2016 14:32, Vincent Cestmois wrote:

> I'd like to merge several files that are like this
> <xml>
>   <catalog>
>     <product>
>       <size>
>        <length>52</length>
>        <width>52</width>
>       </size>
>       <name>My product</name>
>     </product>
>   </catalog>
> </xml>
>
> to get a file like that
>
> <xml>
>   <catalog>
>     <product>
>       <size>
>        <length>52</length>
>        <width>52</width>
>       </size>
>       <name>My product</name>
>     </product>
> <product>
>       <size>
>        <length>25</length>
>        <width>32</width>
>       </size>
>       <name>My product B</name>
>     </product>
>   </catalog>
> </xml>
>
> I've seen on stack overflow I could create a xslt for this, but as I
> need to merge hundreds of file, I would need a way to pass all the
> filenames at one, rather than having to issue hundreds of transforms

As for the XSLT problem, you would need to explain whether you simply
want to insert all "product" elements from all files into a new file or
whether you need to merge data for instance for "product" elements with
the same name.

As for pulling in hundreds of files with Saxon 9, use
http://saxonica.com/html/documentation/functions/fn/uri-collection.html 
and e.g.

<xsl:for-each select="uri-collection('.?select=*.xml')">
   <xsl:source-document href="{.}" streamable="yes">
      <xsl:copy-of select="//product"/>
   </xsl:source-document>
</xsl:for-each>

to copy all "product" elements of all "*.xml" files to the result.


------------------------------------------------------------------------------
_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Vincent Cestmois
In reply to this post by Vincent Cestmois

>As for the XSLT problem, you would need to explain whether you simply want to insert all "product" > elements from all files into a new file

This one, just like a cut and paste

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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Martin Honnen-2
On 07.09.2016 15:12, Vincent Cestmois wrote:
>
>>As for the XSLT problem, you would need to explain whether you
> simply want to insert all "product" > elements from all files into a new
> file
>
> This one, just like a cut and paste

Then I would try along the lines of

<xsl:template name="xsl:initial-template">
<xml>
   <catalog>
     <xsl:for-each select="uri-collection('.?select=*.xml')">
      <xsl:source-document href="{.}" streamable="yes">
       <xsl:copy-of select="//product"/>
      </xsl:source-document>
     </xsl:for-each>
   </catalog>
</xml>
</xsl:template>

and call Saxon 9.7 EE with e.g. -it -xsl:sheet.xsl.

Obviously you would need to adapt the argument of uri-collection to
select the files with the pattern of file names you are interested in.


------------------------------------------------------------------------------
_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Vincent Cestmois
In reply to this post by Vincent Cestmois
Many thanks Martin,

But it says 

Le préfixe "xsl" de l'élément "xsl:for-each" n'est pas lié

The prefix "xsl" of the "xsl:for-each" element is not linked

I've created a merge.xslt with your example, and put it in the same folder were there's the files to merge 

So I must be doing something wrong

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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Martin Honnen-2
On 07.09.2016 16:20, Vincent Cestmois wrote:

> But it says
>
> Le préfixe "xsl" de l'élément "xsl:for-each" n'est pas lié
>
> The prefix "xsl" of the "xsl:for-each" element is not linked
>
> I've created a merge.xslt with your example, and put it in the same
> folder were there's the files to merge
>
> So I must be doing something wrong

Well, the snippet is obviously not a full stylesheet, you need

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:template name="xsl:initial-template">
<xml>
    <catalog>
      <xsl:for-each select="uri-collection('.?select=*.xml')">
       <xsl:source-document href="{.}" streamable="yes">
        <xsl:copy-of select="//product"/>
       </xsl:source-document>
      </xsl:for-each>
    </catalog>
</xml>
</xsl:template>
</xsl:stylesheet>

to make it into a full 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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

dwtung
In reply to this post by Vincent Cestmois
unsubscribe

On Wed, Sep 7, 2016 at 7:20 AM, Vincent Cestmois <[hidden email]> wrote:
Many thanks Martin,

But it says 

Le préfixe "xsl" de l'élément "xsl:for-each" n'est pas lié

The prefix "xsl" of the "xsl:for-each" element is not linked

I've created a merge.xslt with your example, and put it in the same folder were there's the files to merge 

So I must be doing something wrong

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

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



--
Doug Tung
Internet Systems Group, Inc.
2801 Ocean Park Blvd., #248
Santa Monica, CA 90405
310-845-6474

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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

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

unfortunately it still complains that xsl:stylesheet is not linked

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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Michael Kay
In reply to this post by Vincent Cestmois

> On 7 Sep 2016, at 15:20, Vincent Cestmois <[hidden email]> wrote:
>
> Many thanks Martin,
>
> But it says
>
> Le préfixe "xsl" de l'élément "xsl:for-each" n'est pas lié
>
> The prefix "xsl" of the "xsl:for-each" element is not linked

I think the usual translation here would be "is not declared".

This is a basic XML error, if you look at any example of an XSLT stylesheet, you will see that it starts with

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

It's also a basic principle when you ask for help on a mailing list that when you tell us something doesn't work, you show us your code so we can explain what you have done wrong. It's very frustrating not being able to help you simply because you aren't showing us your code.

Michael Kay
Saxonica

------------------------------------------------------------------------------
_______________________________________________
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
|  
Report Content as Inappropriate

Re: Can saxon merge severa xml files, and how

Martin Honnen-2
In reply to this post by Vincent Cestmois
On 08.09.2016 00:46, Vincent Cestmois wrote:

> unfortunately it still complains that xsl:stylesheet is not linked

I am not sure what could be wrong, I took the posted stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="3.0">
        <xsl:template name="xsl:initial-template">
                <xml>
                        <catalog>
                                <xsl:for-each select="uri-collection('.?select=*.xml')">
                                        <xsl:source-document href="{.}" streamable="yes">
                                                <xsl:copy-of select="//product"/>
                                        </xsl:source-document>
                                </xsl:for-each>
                        </catalog>
                </xml>
        </xsl:template>
</xsl:stylesheet>

and saved it as sheet1.xsl in a new directory, added two files doc1.xml
and doc2.xml with some sample data taken from your original question and
run Saxon 9.7.0.8 EE Java from the command line in that directory with
options
   -it -xsl:.\sheet1.xsl -t

and it indicates it compiles the stylesheet fine and then executes it by
loading the two files with streamed processing, giving a result

<?xml version="1.0" encoding="UTF-8"?><xml><catalog><product>
                         <size>
                                 <length>52</length>
                                 <width>52</width>
                         </size>
                         <name>My product</name>
                 </product><product>
                         <size>
                                 <length>25</length>
                                 <width>32</width>
                         </size>
                         <name>My product B</name>
                 </product></catalog>


So as far as I can tell, what I suggested works. If you still have
problems then please explain detail what you are doing different from
what I suggested and in what way it goes wrong.



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