我有很多XML文件,需要将它们合并为一个: 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>
我首先需要在name元素中添加一个值,以便知道它来自哪个文件,然后合并所有xml文件。
所需的输出:
<?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>
在给定的文件夹中,我需要将所有xml文件合并为一个。只是合并他们的内容。没有检查或更新。另外,我需要保留name元素,每个文件都来自该文件,以供将来参考
这是我的审判。 为了使用最新的xslt-3,并需要给定文件夹中的xml文件,我需要经验丰富的帮助。
<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>
答案 0 :(得分:0)
XSLT中有多种方法可以执行此操作,其中一种方法是尝试新的xsl:merge
指令,但是当我在Saxon 9.8中使用该指令时遇到问题(请参阅https://saxonica.plan.io/issues/3883和{{3} })是另一种方式,似乎您只是想复制某个级别的所有元素,就您而言,就是复制第三级别的food
元素;一个通用的样式表来执行此操作,该操作将选择表达式作为静态参数(我将其通用表示为*/*/*
,但是您当然可以将其拼写为menu/breakfast_menu/food
的文档),一个URI和一个输入文件的文件名模式,然后以xsl:initial-template
(Saxon命令行的命令行选项-it
)启动,如下所示:
<?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>
通过简单地调整参数并可能通过调整<xsl:template _match="{$xslt-pattern-to-add-file-name}">
模板的主体,它应该可以正常工作,因为我选择在注释中输出文件名,而不是将其扔到元素的内容中。 / p>
对于在样式表hotel-stackoverflow-test
所在的子目录append.xsl
和Saxon命令行-it -xsl:.\append.xsl input-uri=hotel-stackoverflow-test file-pattern=hotel*.xml
中的三个示例,我得到了输出
<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>
代码应与Saxon 9.8 EE一起使用流传输,并与Saxon 9.8 HE或PE一起使用常规XSLT处理。