2016-08-31 4 views
0

両方のxmlの属性を一致させることによって、xmlの属性の値を別のxmlの属性で更新しようとしています。2 xmlの属性の一致と更新

XML1:

<?xml version="1.0" encoding="utf-8"?> 
<Products> 
    <Product> 
     <List prodId="123456" sellId="">   
     </List> 
    </Product> 
</Products> 

XML2:

<?xml version="1.0" encoding="utf-8"?> 
<Products> 
    <Product> 
     <info prodId="123456" sellId="121">   
      <qnty>4</qnty> 
     </info> 
     <info prodId="23456" sellId="890">   
      <qnty>1</qnty> 
     </info> 
    </Product> 
</Products> 

私はPRODID属性によって、第2のXMLでは、ノードする必要があり、そのノードから私は属性sellId = "890" を取る必要があり、最初のxmlに移入します。ご希望の場合は、

<xsl:template match="List" > 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:variable name="tprodId" select="@prodId"/> 
     <xsl:for-each select="$doc1"> 
      <xsl:copy-of select="key('k1', $tprodId)/@sellId"/>     
     </xsl:for-each> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

または:

所望の出力:

<?xml version="1.0" encoding="utf-8"?> 
<Products> 
    <Product> 
     <List prodId="123456" sellId="890">   
     </List> 
    </Product> 
</Products> 

は、これは私のXSL

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

    <xsl:param name="f1" select="'xml2.xml'"/>  
    <xsl:variable name="doc1" select="document($f1)"/> 

    <xsl:key name="k1" match="Products/Product/info" use="@prodId"/> 

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

    <xsl:template match="Products/Product/List"> 

     <xsl:variable name="tprodId" select="@prodId"/> 
     <xsl:for-each select="$doc1"> 
     <xsl:attribute name="sellId"> 
       <xsl:value-of select="key('k1', $tprodId)/@sellId"/> 
       </xsl:attribute>     
     </xsl:for-each>  

    </xsl:template> 

答えて

0

である私は、あなたがそれをこの方法で行うことをお勧め:

<xsl:template match="@sellId" > 
    <xsl:variable name="tprodId" select="../@prodId"/> 
    <xsl:for-each select="$doc1"> 
     <xsl:copy-of select="key('k1', $tprodId)/@sellId"/>     
    </xsl:for-each>   
</xsl:template> 
+0

なぜxpathからすべてを削除しましたか?簡単にするために? (上記の例は簡略化されたバージョンであり、複数のノードを持つことができるため) ランタイムエラー:ファイルcopy.xsl 12行目の要素コピー 子ノードの前に属性ノードを要素に追加する必要があります。 – yuris

+0

2番目の例が動作します – yuris

+0

@yurisツリーの異なる枝に複数の 'List'要素がある場合、それらの間を区別するのに十分な固有の一致パターンを作成する必要があります。 –

関連する問題