2016-09-01 3 views
0

これはfollowupの質問です。子ノードから2 xmlの一致するノードのチャイルドノードに属性をコピーします

私は今コピーする必要のある属性を持つxml2に子ノードを持っています。

XML1

<?xml version="1.0" encoding="utf-8"?> 
<Products> 
    <Product prodId="123456" sellId="" colorId="">     
      <Supplier id="" name=""/> 
      <Misc lib="" />      
    </Product> 
</Products> 

XML2

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

今回、XML2のノードのqnty」の 'libに' 属性は、ノード 'その他' の 'LIB' 属性に行く必要があります。

は今、私のテンプレートは、検索

<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" > 
       <xsl:copy> 
         <xsl:apply-templates select="@*"/> 
         <xsl:variable name="prodId" select="@prodId"/> 
         <xsl:for-each select="$doc1"> 
           <xsl:copy-of select="key('k1', $prodId)/@sellId"/> 
           <xsl:copy-of select="key('k1', $prodId)/@colorId"/>         

             <xsl:apply-templates select="Products/Product/Misc"/> 
             <xsl:copy-of select="key('k1', $prodId)/qnty/@lib"/>   
         </xsl:for-each> 
         <xsl:apply-templates/> 
       </xsl:copy> 
     </xsl:template> 

「libに」属性は「製品」ノードにではなく、子供に「その他」ノードを追加されます。

答えて

0

それが最善の解決策だと確信していない、次のように解決:

<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" > 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:variable name="prodId" select="@prodId"/> 
      <xsl:for-each select="$doc1"> 
       <xsl:copy-of select="key('k1', $prodId)/@sellId"/> 
       <xsl:copy-of select="key('k1', $prodId)/@colorId"/>            
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 


    <xsl:template match="Products/Product/Misc" > 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:variable name="prodId" select="../@prodId"/> 
      <xsl:for-each select="$doc1">            
        <xsl:copy-of select="key('k1', $prodId)/qnty/@lib"/>  
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
関連する問題