如何快速将一个文件的XML内容添加到另一个XML文件(性能)?

问题描述 投票:-1回答:1

我有2个XML文件。一个是主XML文件,另一个是附加XML文件。这些文件具有特定格式,如下例所示。

main.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<nation xmlns="http://abc.def.ghi">
  <library>
    <book>
      <title name="Alice in Wonderland"/>
      <authors>
        <author order="1" name="Alex, A."/>
        <author order="2" name="Bob, B."/>
        <author order="3" name="Carrot, C."/>
      </authors>
      <notes>
       <note description='Secret ID: 123456'/>
      </notes>
    </book>
    <novel>
      <title name="Bob in Wonderland"/>
      <authors>
        <author order="1" name="Zen, Z."/>
      </authors>
      <notes>
       <note description='Secret ID: 123457'/>
      </notes>
    </novel>
  </library>
</nation>

additional.xml:

<additionalAutors>
  <authorList description='Secret ID: 123456'>
    <author order="4" name="Den, D."/>
    <author order="5" name="East, E."/>
  </authorList>
  <authorList description='Secret ID: 123457'>
    <author order="2" name="Fan, F."/>
  </authorList>
</additionalAutors>

注意:可以有很多书和小说。除了书籍和小说之外,还有更多的东西,比如报纸,词典......等等

我想将附加XML文件中包含的信息添加到主XML文件中,如下所示:

为result.xml:

<?xml version="1.0" encoding="UTF-8"?>
<nation xmlns="http://abc.def.ghi">
  <library>
    <book>
      <title name="Alice in Wonderland"/>
      <authors>
        <author order="1" name="Alex, A."/>
        <author order="2" name="Bob, B."/>
        <author order="3" name="Carrot, C."/>
        <author order="4" name="Den, D."/>
        <author order="5" name="East, E."/>
      </authors>
      <notes>
       <note description='Secret ID: 123456'/>
      </notes>
    </book>
    <novel>
      <title name="Bob in Wonderland"/>
      <authors>
        <author order="1" name="Zen, Z."/>
        <author order="2" name="Fan, F."/>
      </authors>
      <notes>
       <note description='Secret ID: 123457'/>
      </notes>
    </novel>
  </library>
</nation>

注意:逻辑是将具有相同机密ID的作者从附加文件添加到主文件:

我需要快速完成,因为有性能要求。我正在使用XSLT来完成这项工作。如果所有这些文件都在同一个目录中,我的xsl文件(让我们称之为solution.xsl)应该怎么样?

如果提到的所有文件都在同一个文件夹中,我希望能够在我的Mac上运行以下行:

xsltproc -o new_main.xml solution.xsl main.xml

提前致谢!

更新1:包含我的代码:

嗨Amrendra Kumar,这是我的代码。它真的远没有完成,但还没有多大意义:

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
  <output indent="yes" method="xml"/>

  <template match="@*|node()">
    <copy>
      <apply-templates select="@*|node()"/>
    </copy>
  </template>

  <template match="library">
    <copy>
      <apply-templates select="@*|node()"/>
      <for-each select="document('additional.xml')/library/book">
        <copy>
          <apply-templates select="@*|node()"/>
        </copy>
      </for-each>
    </copy>
  </template>

</stylesheet>

更新2:如果可能,请提供用XSLT版本1编写的答案,因为我在Mac上使用xsltproc

更新3:我找到了解决方案,但要使用它,我将不得不修改我的additional.xml文件(我编辑它就可以了):

additional.xml:

<?xml version="1.0" encoding="UTF-8"?>
<additionalAutors xmlns="http://add.authors.com">
  <authorList description='Secret ID: 123456'>
    <author order="4" name="Den, D."/>
    <author order="5" name="East, E."/>
  </authorList>
  <authorList description='Secret ID: 123457'>
    <author order="2" name="Fan, F."/>
  </authorList>
</additionalAutors>

在学习了Amrendra Kumar的代码之后,我能够提出真正解决我问题的解决方案:

solution.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:abc="http://abc.def.ghi"
  xmlns:add="http://add.authors.com"
  exclude-result-prefixes="abc add"
  version="1.0">

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:variable name="additional" select="document('additional.xml')" as="node()"/>

    <xsl:template match="abc:authors">
        <authors xmlns="http://abc.def.ghi">
            <xsl:apply-templates select="abc:author"/>
            <xsl:variable name="aut-name" select="following-sibling::abc:notes[1]/abc:note/@description"/>
            <xsl:apply-templates select="$additional/descendant::*[name() = 'author' and parent::*[name() = 'authorList']/@description = $aut-name]">
                <xsl:sort select="@order"/>
            </xsl:apply-templates>
        </authors>
    </xsl:template>

    <xsl:template match="add:author">
      <xsl:element name="author" use-attribute-sets="order" namespace="http://abc.def.ghi">
        <xsl:attribute name = "order">
          <xsl:value-of select = "@order"/>
        </xsl:attribute>
        <xsl:attribute name = "name">
          <xsl:value-of select = "@name"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
xml xslt
1个回答
2
投票

你可以用这个:

<?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="http://abc.def.ghi"
    xpath-default-namespace="http://abc.def.ghi"
    exclude-result-prefixes="xs #default"
    version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:variable name="additional" select="document('additional.xml')" as="node()"/>

    <xsl:template match="authors">
        <authors>
            <xsl:apply-templates/>
            <xsl:variable name="aut-name" select="following-sibling::notes[1]/note/@description"/>
            <xsl:apply-templates select="$additional/descendant::*[name() = 'author' and parent::*[name() = 'authorList']/@description = $aut-name]">
                <xsl:sort select="@order"/>
            </xsl:apply-templates>
        </authors>
    </xsl:template>


</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.