使用XSLT合并2 XML文档中的行

时间:2013-04-29 10:02:49

标签: xml xslt

我需要一些帮助来使用XSLT合并2个xml文件。下面我放置一些示例文件。 请按照以下

第一个XML文件(1.xml)

<?xml version="1.0" encoding="UTF-8"?>
<rowset  xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row>
    <Column0>1</Column0>
    <Column1>aaa</Column1>
    <Column2>a1</Column2>
</Row>
<Row>
    <Column0>2</Column0>
    <Column1>bbb</Column1>
    <Column2>b1</Column2>
</Row>
<Row>
    <Column0>3</Column0>
    <Column1>ccc</Column1>
    <Column2>c1</Column2>
</Row>

</rowset>

第二个XML文件(2.xml)

<?xml version="1.0" encoding="UTF-8"?>
<rowset  xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Row>
    <Column0>3</Column0>
    <Column1>ddd</Column1>
    <Column2>d1</Column2>
</Row>
<Row>
    <Column0>4</Column0>
    <Column1>eee</Column1>
    <Column2>e1</Column2>
</Row>
<Row>
    <Column0>1</Column0>
    <Column1>aaa</Column1>
    <Column2>a1</Column2>
</Row>

</rowset>

预期产出

<?xml version="1.0" encoding="UTF-8"?>
<rowset  xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row>
    <Column0>1</Column0>
    <Column1>aaa</Column1>
    <Column2>a1</Column2>
</Row>
<Row>
    <Column0>2</Column0>
    <Column1>bbb</Column1>
    <Column2>b1</Column2>
</Row>
<Row>
    <Column0>3</Column0>
    <Column1>ccc</Column1>
    <Column2>c1</Column2>
</Row>

<Row>
    <Column0>3</Column0>
    <Column1>ddd</Column1>
    <Column2>d1</Column2>
</Row>
<Row>
    <Column0>4</Column0>
    <Column1>eee</Column1>
    <Column2>e1</Column2>
</Row>
<Row>
    <Column0>1</Column0>
    <Column1>aaa</Column1>
    <Column2>a1</Column2>
</Row>

</rowset>

任何人都可以帮助我......


谢谢, JP

1 个答案:

答案 0 :(得分:0)

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml"/>
  <xsl:param name="file1" select="document('Untitled1.xml')"/>
  <xsl:param name="file2" select="document('Untitled2.xml')"/>
  <xsl:key name="Column" match="*[starts-with(name(), 'Column')]" use="self::*"/>
  <xsl:template match="/">
    <rowset  xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:copy-of select="$file1//*:Row"/>
    <xsl:copy-of select="$file2//*:Row"/>
      </rowset>
  </xsl:template>
</xsl:stylesheet>