2016-08-31 1 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 :

I는 PRODID 속성에 의해 제 XML의 노드해야하고, 그 노드에서 I는 속성을 취할 필요
<?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> 

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

두 번째 예제가 작동합니다. – yuris

+0

@yuris 트리의 다른 분기에 여러 개의 List 요소가있는 경우 - 그 다음에 만 - 일치 패턴을 구별 할 수있을만큼 고유하게 만들 필요가 있습니다. –