2

我有两个 xml 文件。我想将一个 xml 文件数据复制到最后一次出现的元素下的另一个文件中。以下是我拥有的 xml:

---------- xml-1---------------
<?xml version="1.0"?>
<parent>
....
<form id="1" name="world"/>
 <source id="1" name="abc1"/>
 <source id="2" name="abc2"/>
 <source id="3" name="abc3"/>
 <file id="1" name="xyz"/>
....
</parent> 

----------- xml-2--------------
<?xml version="1.0"?>
<root>
<source id="4" data="anything"/>
<source id="5" data="anything"/>
<source id="6" data="anything"/>
<source id="7" data="anything"/>
</root>


------------------The desired output I want-----------------
<?xml version="1.0"?>
<parent>
....
<source id="1" name="abc1"/>
<source id="2" name="abc2"/>
<source id="3" name="abc3"/>
<source id="4" data="anything"/>
<source id="5" data="anything"/>
<source id="6" data="anything"/>
<source id="7" data="anything"/>
<file id="1" name="xyz"
....
</parent> 



============== xslt I am using =============

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="source">
    <xsl:choose>
      <xsl:when test="'id=3'">
          <xsl:call-template name="identity"/>
          <xsl:copy-of select="document('file:///D:/Softwares/JEE    eclipse/JEEworkspace/Migration/TestMigrationWithoutDeletingProject/xml2.xml')/*/*"/>
      </xsl:when>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

我试图根据 id 属性找出它,但它在 xml-1 数据的每个源元素之后复制 xml-2 数据。请帮助我。

4

1 回答 1

0

我认为您的代码中有两个小问题:

  • 调用<xsl:call-template name="identity"/>应该在您的choose部分之外(或更确切地说是之前)。顺便说一句:因为你没有一个otherwise部分,所以在这里使用会稍微短一些xsl:if
  • 正确插入点的测试应该是test="@id = 3"因为该术语"'id=3'"只是一个总是产生的字符串true()
于 2013-10-23T23:03:35.697 回答