我有两个xml文件.我需要将它们合并在一起,其中元素“myid”在两者之间匹配.请看一下这些示例文件……
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
原文链接:https://www.f2er.com/xml/292599.htmlhttp://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>
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:
<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>