2016-09-01 4 views
0

Dies ist eine followup Frage.Attribute von untergeordneten Knoten in einen chiled Knoten von übereinstimmenden Knoten in 2 xmls kopieren

Diesmal habe ich einen Kindknoten in xml2 mit Attributen, die ich kopieren muss.

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> 

Diesmal 'lib' Attribut des Knotens 'Qnty' von xml2 zu 'lib' Attribut des Knotens 'Verschiedenes' gehen sollte.

Nun, meine Vorlage, sucht

<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‘ Attribut auf ‚Produkt‘ Knoten hinzugefügt wird, und nicht auf das Kind ‚Verschiedenes‘ Knoten.

Antwort

0

wie diese gelöst werden, nicht sicher, es ist die beste Lösung:

<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> 
Verwandte Themen