简体   繁体   中英

how to add up attributes present in two xml using xslt

I have these two xml ,and I want the sum of attibutes in final xml by summing them from given two inputs.Explained below in example

<?xml version="1.0" encoding="UTF-8"?>
<chart xmlns="http://www.xyz.in/server/model" labelStep="1" showValues="0">
   <categories>
      <category Label="Bangalore Technical RATH" />
   </categories>
   <dataset>
      <set value="3" anchorRadius="2" anchorBorderThickness="3" />
      <set value="3" anchorRadius="2" anchorBorderThickness="3" />
   </dataset>
</chart>

similar is second input2.xml

<?xml version="1.0" encoding="UTF-8"?>
<chart xmlns="http://www.xyz.in/server/model" labelStep="1" showValues="0">
   <categories>
      <category Label="Bangalore Technical RATH" />
   </categories>
   <dataset>
      <set value="2" anchorRadius="2" anchorBorderThickness="3" />
      <set value="1" anchorRadius="2" anchorBorderThickness="3" />
   </dataset>
</chart>

Code I am using for xsl-ttransformation is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:variable name="second" select="document('file2.xml')//*[local-name()='set']" />
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="*[local-name()='set']/@*[local-name()='value']">
      <xsl:for-each select="//*[local-name()='set']">
         <xsl:variable name="secondvalue" select="$second/@value" />
         <xsl:attribute name="value">
            <xsl:value-of select="@*[local-name()='value']  + $secondvalue[1]" />
         </xsl:attribute>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

so in final output.xml I need showing only part Interested in

<dataset>
       <set value="5" anchorRadius="2" anchorBorderThickness="3"/>
       <set value="4" anchorRadius="2" anchorBorderThickness="3"/>
</dataset>

How about this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.xyz.in/server/model" version="1.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="my:dataset">
      <xsl:copy>
         <xsl:for-each select="my:set">
            <xsl:variable name="position" select="position()" />
            <xsl:copy>
               <xsl:apply-templates select="@*" />
               <xsl:attribute name="value">
                  <xsl:value-of select="number(@value) + number(document('source2.xml')/my:chart/my:dataset/my:set[$position]/@value)" />
               </xsl:attribute>
            </xsl:copy>
         </xsl:for-each>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

Try setting the sum in cache and later when you are parsing the resultant query, you can use this cache to put the sum of the values from other 2 xmls. Also you can set profile attribute for the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM