2015-06-20 136 views
0

对于XSLT来说非常新颖,并且在转换XML时需要一些帮助。以下XML可以有多个“行”标签XSLT通过重复节点将节点中的值复制到另一节点

<?xml version="1.0" encoding="UTF-8"?> 
    <ns1:row> 
     <ns1:City>BALTIMORE</ns1:City> 
     <ns1:Miscdata> 
      <ns1:Building> 
       <ns1:VendorCode>123</ns1:VendorCode> 
       <ns1:Value>2</ns1:Value> 
      </ns1:Building> 
      <ns1:Building> 
       <ns1:VendorCode>345</ns1:VendorCode> 
       <ns1:Value>8</ns1:Value> 
      </ns1:Building> 
     </ns1:Miscdata> 
</ns1:row> 

    <ns1:row> 
      <ns1:City>FREMONT</ns1:City> 
      <ns1:Miscdata> 
       <ns1:Building> 
        <ns1:VendorCode>332</ns1:VendorCode> 
        <ns1:Value>4</ns1:Value> 
       </ns1:Building> 
       <ns1:Building> 
        <ns1:VendorCode>342</ns1:VendorCode> 
        <ns1:Value>14</ns1:Value> 
       </ns1:Building> 
       <ns1:Building> 
        <ns1:VendorCode>323</ns1:VendorCode> 
        <ns1:Value>233</ns1:Value> 
       </ns1:Building> 
      </ns1:Miscdata> 
    </ns1:row> 

上述XML中“VendorCode”标签中的值需要复制到“Value”标签。输出XML是

<?xml version="1.0" encoding="UTF-8"?> 
<ns1:row> 
     <ns1:City>BALTIMORE</ns1:City> 
     <ns1:Miscdata> 
      <ns1:Building> 
       <ns1:VendorCode>123</ns1:VendorCode> 
       <ns1:Value>123</ns1:Value> 
      </ns1:Building> 
      <ns1:Building> 
       <ns1:VendorCode>345</ns1:VendorCode> 
       <ns1:Value>345</ns1:Value> 
      </ns1:Building> 
     </ns1:Miscdata> 
</ns1:row> 
<ns1:row> 
     <ns1:City>FREMONT</ns1:City> 
     <ns1:Miscdata> 
      <ns1:Building> 
       <ns1:VendorCode>332</ns1:VendorCode> 
       <ns1:Value>332</ns1:Value> 
      </ns1:Building> 
      <ns1:Building> 
       <ns1:VendorCode>342</ns1:VendorCode> 
       <ns1:Value>342</ns1:Value> 
      </ns1:Building> 
      <ns1:Building> 
       <ns1:VendorCode>323</ns1:VendorCode> 
       <ns1:Value>323</ns1:Value> 
      </ns1:Building> 
     </ns1:Miscdata> 
</ns1:row> 

回答

0

所有这些任务使用模板

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

为出发点,那么你对于例如需要特殊处理节点添加模板在你的情况下

<xsl:template match="ns1:Building/ns1:Value"> 
    <xsl:copy> 
    <xsl:value-of select="preceding-sibling::ns1:VendorCode"/> 
    </xsl:copy> 
</xsl:template> 
+0

非常感谢你马丁。有效! –