簡體   English   中英

如何使用xslt合並每個元素的兩個xml文件

[英]How to merge two xml files each element with xslt

我有兩個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 =======> 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>

=======> 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>

<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"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM