XSLT:合并xml文件的简单方法 [英] XSLT: A simple way to merge xml files

查看:32
本文介绍了XSLT:合并xml文件的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two xml files. I need to merge them together where the element "myid" matches between the two. Please have a look at these example files...

File1.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>

File2.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

I have been researching a bit and found a fairly similar question here: http://forums.tizag.com/showthread.php?p=76699

这是我想出的,这似乎主要是有效的,除了 Firefox 没有将其识别为 xml 文件,即使我添加了 xml:output.

Here is what I have come up with, this seems to be mostly working except that Firefox is not recognizing it as an xml file even though I have added the xml:output.

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

File1.xml (note line two, referencing our transformation):

<?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>

File2.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>

merge.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:

Output xml when viewing File1.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>

这篇关于XSLT:合并xml文件的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆