如何使用xslt合并每个元素的两个xml文件

时间:2017-04-05 14:00:12

标签: xml xslt merge

我有两个xml文件,需要使用XSLT合并为一个。我试图找到解决方案,但没有成功

file1 =======> xpto.xml     

<Rows>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
    </Row>
</Rows>

file2 =======&gt; links.xml     

<Rows>
    <Row><link>site1</link></Row>
    <Row><link>site2</link></Row>
    <Row><link>site3</link></Row>
    <Row><link>site4</link></Row>
</Rows>

所需的合并输出:result.xml

<?xml version="1.0" encoding="utf-8"?>

<Rows   
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
        <link>site1</link>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
        <link>site2</link>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
        <link>site3</link>
    </Row>
    <Row>
        <Model>INTEGRA 256 Plus</Model> 
        <Descri>EN-50131 GRADE 3</Descri> 
        <Update>updated: 2014-06-23</Update> 
        <Filesize>1.47 MB</Filesize>
        <link>site4</link>
    </Row>
</Rows>

=======&GT; merge.xsl(解决我得到的,但不是我想要的)

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:param name="fileName" select="'links.xml'" />
<xsl:param name="updates" select="document($fileName)" />

<xsl:variable name="updateLinks" select="$updates/Rows/Row" />

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Row">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
        <xsl:copy-of select="$updateLinks/*"></xsl:copy-of>
    </xsl:copy>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

<xsl:copy-of select="$updateLinks/*">修改为

<xsl:copy-of select="$updates//Row[position() = $rowposition]/link">

并在<xsl:template match="Row">添加

下方
<xsl:variable name="rowposition" select="count(preceding-sibling::*)+1"/>
相关问题