2009-10-02 69 views
8

我有两个xml文件。我需要将它们合并到两个元素“myid”匹配的位置。请看看这些示例文件...XSLT:合并xml文件的简单方法

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> 
+0

相关:http://stackoverflow.com/questions/1430710/two-xml-in-one-xslt – 2009-10-02 17:05:54

+1

@dacracot:输入文件格式不正确。 -------- @ nicholas.alipaz:我们是否看到摘录,而不是整个文件? – 2009-10-02 17:21:33

+0

我已更新我的帖子。对困惑感到抱歉。 – 2009-10-02 18:15:55

回答

4

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

下面是我想到的,这似乎主要是工作,除了Firefox不认为它是一个XML文件,即使我已经添加了XML:输出。

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> 

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> 

输出XML观看时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> 
+0

404;帖子不存在 – 2013-03-13 14:59:08

+0

嗨c#代码请为此 – Lijo 2017-02-28 07:31:23

+0

@AdamLynch对不起,我寻找缓存版本的帖子,但空了。由于我列出了上面所需的一切,因此并不太重要。最好! – 2017-03-02 23:24:00