xml - XSLT:合并 xml 文件的简单方法

标签 xml xslt

我有两个 xml 文件。我需要将它们合并在一起,其中元素“myid”在两者之间匹配。请查看这些示例文件...

文件1.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
  </data>

  <data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
  </data>
</catalog>

文件2.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <data>
    <author>Author1</author>
    <date>12/34/5678</date>
    <myid>1</myid>
  </data>

  <data>
    <author>Author2</author>
    <date>87/65/4321</date>
    <myid>2</myid>
  </data>
</catalog>

生成的文件如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
    <author>Author1</author>
    <date>12/34/5678</date>
  </data>

  <data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
    <author>Author2</author>
    <date>87/65/4321</date>
  </data>
</catalog>

最佳答案

我一直在研究,在这里发现了一个非常相似的问题: http://forums.tizag.com/showthread.php?p=76699

这是我想出的,这似乎主要是工作,除了 Firefox 没有将它识别为 xml 文件,即使我已经添加了 xml:output。

File1.xml(注意第二行,引用我们的转换):

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl"?>
<catalog>
  <data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
  </data>

  <data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
  </data>
</catalog>

文件2.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <data>
    <author>Author1</author>
    <date>12/34/5678</date>
    <myid>1</myid>
  </data>

  <data>
    <author>Author2</author>
    <date>87/65/4321</date>
    <myid>2</myid>
  </data>
</catalog>

合并.xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
  <xsl:variable name="with" select="'File2.xml'" />

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

  <xsl:template match="scene">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="info" select="document($with)/catalog/data[myid=current()/myid]/." />
      <xsl:for-each select="$info/*">
        <xsl:if test="name()!='myid'">
          <xsl:copy-of select="." />
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

查看File1.xml时输出xml:

<catalog>
  <data>
    <title>Title1</title>
    <description>Description1</description>
    <myid>1</myid>
    <author>Author1</author>
    <date>12/34/5678</date>
  </data>

  <data>
    <title>Title2</title>
    <description>Description2</description>
    <myid>2</myid>
    <author>Author2</author>
    <date>87/65/4321</date>
  </data>
</catalog>

关于xml - XSLT:合并 xml 文件的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1510688/

相关文章:

java - Android 通过 java 代码更改 XML 文件的背景

xml - XSL转换: Using translate and substring-after in XSL

xslt - 使用 XSLT/XPath 进行日期格式化

xml - 基于预定义输出字符串的 XSLT 转换

xml - XSLT - 在展平 xml 后组合多个属性(连接)

php - 如何从 PHP 解析以文本/xml 形式发送到服务器的 XML?

xml - 如何通过 XPath 访问具有多个命名空间的 XML 中的元素?

java - 使用 Jackson XML 映射器将 Java 列表序列化为 XML

xml - xslt分组问题

xslt 命名模板 : how to write a template that creates a string representing the xpath of a given node