首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xslt-3合并两个或多个xml文件

使用xslt-3合并两个或多个xml文件
EN

Stack Overflow用户
提问于 2018-08-23 20:28:42
回答 1查看 725关注 0票数 0

我有许多XML文件,需要将它们合并到一个文件中: to 1.xml

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

System3.xml:

代码语言:javascript
复制
<?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文件。

所需输出:

代码语言:javascript
复制
<?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文件存在于给定的文件夹。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-24 23:59:44

在XSLT中有多种方法可以做到这一点,一种是尝试新的xsl:merge指令,但是当我在使用Saxon9.8(参见https://saxonica.plan.io/issues/3883https://saxonica.plan.io/issues/3884)时遇到问题时,这里是一种不同的方法,看起来你只是想复制来自某一层的所有元素,在你的例子中是来自第三层的food元素;执行此操作的通用样式表将select表达式作为静态参数(我将其作为*/*/*通用,但您当然可以将其拼写为menu/breakfast_menu/food)、URI和输入文件的文件名模式,然后以xsl:initial-template (Saxon命令行的命令行选项-it )启动,如下所示:

代码语言:javascript
复制
<?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}">模板的主体,因为我选择了在注释中输出文件名,而不是将其放入元素的内容中。

对于样式表append.xsl所在的子目录hotel-stackoverflow-test和Saxon命令行-it -xsl:.\append.xsl input-uri=hotel-stackoverflow-test file-pattern=hotel*.xml中的三个示例,我将获得输出

代码语言:javascript
复制
<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处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51985818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档