xslt-3.0 - 使用 xslt-3 合并两个或多个 xml 文件
问题描述
我有许多 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>
酒店2:
<?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>
酒店3.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 文件合并为一个。只需合并它们的内容。没有检查或更新。另外,我需要保留名称元素,每个来自的文件,以供将来参考
这是我的审判。我需要更有经验的人帮助,以便使用最新的 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>
解决方案
XSLT 中有多种方法可以做到这一点,一种是尝试新xsl:merge
指令,但是当我在 Saxon 9.8 中使用它时遇到问题(参见https://saxonica.plan.io/issues/3883和https:// saxonica.plan.io/issues/3884)这是一种不同的方式,似乎您只是想复制某个级别的所有元素,在您的情况下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}">
模板的主体,因为我选择在注释中输出文件名,而不是把它扔到元素的内容中。
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 处理。
推荐阅读
- laravel - 如何在 laravel 中配置重置密码
- java - 为什么需要在java中创建默认构造函数?
- shopify - 在无限主题产品页面中编辑图像大小?
- reactjs - `combineReducers` - 无法理解需求和用法
- eclipse - 无法在 Maven 项目的源代码中命中断点
- javascript - 如何根据php中的前2个输入字段选择值
- java - 无法从我的 WebView 在 Whatsapp 上分享。无法加载位于 whatsapp://send?text= 的网页,因为 net::ERR_UNKNOWN_URL_SCHEME
- angular - 是否可以使 mat-button-toggle-group 响应?
- python - 常数 e 的连分数
- php - 如何从 MySQL 表中选择消息列表?