Reputation:
I have many XML files, which I need to merge into one: hotel1.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
</menu>
hotel2:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<breakfast_menu>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
</menu>
hotel3.xml:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<breakfast_menu>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
</menu>
I need to first add a value to the name element, in order to know from which file it came from, and merge all xml files.
Desired output:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<breakfast_menu>
<food>
<name>Belgian Waffles-hotel1</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles-hotel1</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles-hotel2</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast-hotel3</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast-hotel3</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
</menu>
in a given folder, i need to merge all xml files into one. Just merge their contents. No checks, or updates. Also, i need to keep in the name element, the file each came from, for future reference
Here is my trial. I need someone more experienced help, in order to use latest xslt-3, and for as xml files that exist in a given folder.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="breakfast_menu">
<xsl:copy>
<xsl:apply-templates select="*"/>
<xsl:apply-templates select="document('hotel1.xml')/menu/breakfast_menu/*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 1066
Reputation: 167716
There are various ways in XSLT to do this, one would be to try the new xsl:merge
instruction, but as I run into problem using that with Saxon 9.8 (see https://saxonica.plan.io/issues/3883 and https://saxonica.plan.io/issues/3884) here is a different way, it seems you simply want to copy all the elements from a certain level, in your case the food
elements from the third level; a generic stylesheet to do that which takes a select expression as a static parameter (I have kept it generic as */*/*
but you can of course spell it out for your documents as menu/breakfast_menu/food
), a URI and a file name pattern for the input files and then is started with the xsl:initial-template
(command line option -it
for Saxon's command line) is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:param name="input-uri" as="xs:string" select="'.'"/>
<xsl:param name="file-pattern" as="xs:string" select="'hotel*.xml'"/>
<xsl:param name="merge-select-expression" as="xs:string" static="yes" select="'*/*/*'"/>
<xsl:param name="xslt-pattern-to-add-file-name" as="xs:string" static="yes" select="$merge-select-expression || '/name'"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy" streamable="yes"/>
<xsl:template _match="{$xslt-pattern-to-add-file-name}">
<xsl:comment>Copied this {node-name()} element from {tokenize(document-uri(/), '/')[last()]}</xsl:comment>
<xsl:next-match/>
</xsl:template>
<xsl:template name="xsl:initial-template">
<xsl:sequence select="mf:append-docs(uri-collection($input-uri || '?select=' || $file-pattern))"/>
</xsl:template>
<xsl:function name="mf:append-docs" as="document-node()">
<xsl:param name="doc-uris" as="xs:anyURI+"/>
<xsl:source-document href="{head($doc-uris)}" streamable="yes">
<xsl:apply-templates select="." mode="construct">
<xsl:with-param name="remaining-doc-uris" as="xs:anyURI*" select="tail($doc-uris)" tunnel="yes"/>
</xsl:apply-templates>
</xsl:source-document>
</xsl:function>
<xsl:mode name="construct" on-no-match="shallow-copy" streamable="yes"/>
<xsl:template _match="{string-join(tokenize($merge-select-expression, '/')[position() lt last()], '/')}" mode="construct">
<xsl:param name="remaining-doc-uris" as="xs:anyURI*" tunnel="yes"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
<xsl:for-each select="$remaining-doc-uris">
<xsl:source-document href="{.}" streamable="yes">
<xsl:apply-templates _select="{$merge-select-expression}"/>
</xsl:source-document>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It should work pretty generic by simply adjusting the parameters and perhaps by adjusting the body of the <xsl:template _match="{$xslt-pattern-to-add-file-name}">
template as there I have chosen to output the file name in a comment instead of throwing it into the element's content.
For your three samples in a subdirectory hotel-stackoverflow-test
of where the stylesheet append.xsl
is and the Saxon command line -it -xsl:.\append.xsl input-uri=hotel-stackoverflow-test file-pattern=hotel*.xml
I get the output
<menu>
<breakfast_menu>
<food><!--Copied this name element from hotel1.xml-->
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food><!--Copied this name element from hotel1.xml-->
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food><!--Copied this name element from hotel2.xml-->
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food><!--Copied this name element from hotel3.xml-->
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food><!--Copied this name element from hotel3.xml-->
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
</menu>
The code should use streaming with Saxon 9.8 EE and normal XSLT processing with Saxon 9.8 HE or PE.
Upvotes: 0