2010-12-02 31 views
1

我想用其他xml更新一个xml值。XSLT转换中的多个文件输入

假设我有具有

<Customer> 
    <Fname>John</Fname> 
    <Lname>Smith<Lname> 
</Customer> 

其它XML由具有

<Customer>         
    <Lname>Smith<Lname> 
</Customer> 

我想从第一传送<Fname>John</Fname>到第二XML如果该信息不存在于第二XML根节点一个xml 。

是否可以在.net中使用xslt?

+0

@Jon Smith:如果使用XML1和XML2的转换的结果将是XML1,则不需要转换。请澄清。 – 2010-12-02 13:27:43

回答

3

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="kElementByAncestors" match="*" 
      use="concat(name(../..),'+',name(..))"/> 
    <xsl:key name="kAttributeByAncestors" match="@*" 
      use="concat(name(../..),'+',name(..))"/> 
    <xsl:param name="pSource2" select="'source2.xml'"/> 
    <xsl:variable name="vSource2" select="document($pSource2,/)"/> 
    <xsl:template match="*"> 
     <xsl:variable name="vKey" select="concat(name(..),'+',name())"/> 
     <xsl:variable name="vCurrent" select="."/> 
     <xsl:copy> 
      <xsl:for-each select="$vSource2"> 
       <xsl:variable name="vNames"> 
        <xsl:text>|</xsl:text> 
        <xsl:for-each select="$vCurrent/*"> 
         <xsl:value-of select="concat(name(),'|')"/> 
        </xsl:for-each> 
       </xsl:variable> 
       <xsl:copy-of select="key('kAttributeByAncestors',$vKey)"/> 
       <xsl:copy-of select="$vCurrent/@*"/> 
       <xsl:copy-of 
        select="key('kElementByAncestors', 
           $vKey)[not(contains($vNames, 
                concat('|', 
                  name(), 
                  '|')))]"/> 
      </xsl:for-each> 
      <xsl:apply-templates select="node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

有了这个输入:

<Customer> 
    <Lname>Smith</Lname> 
    <Data>Data</Data> 
</Customer> 

和 “source2.xml”:

<Customer test="test"> 
    <Fname>John</Fname> 
    <Lname>Smith</Lname> 
</Customer> 

输出:

<Customer test="test"> 
    <Fname>John</Fname> 
    <Lname>Smith</Lname> 
    <Data>Data</Data> 
</Customer> 
+0

优秀答案,+1。 – 2010-12-03 05:42:51