Daniel St-Jean
Daniel St-Jean

Reputation: 117

XSLT: Sum of specific attribute value based on other attribute's name

I have the following XML document (provided by a different party)

<transactions>
  <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/>
  <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/>
</transactions>

I would like to write an XSLT document that would transform these lines and sum up the Tax*Value if the corresponding Tax*Type = '11' for example.

Is this something possible without using a template?

name(), substring(), etc. functions in the XQuery? What are your thoughts on this?

Upvotes: 5

Views: 26576

Answers (4)

titan.hi3
titan.hi3

Reputation: 43

sum(transactions/transaction[@TaxAType='11']/@TaxAValue | @TaxBvalue | @ TaxCvalue)

Upvotes: 0

JLRishe
JLRishe

Reputation: 101730

Here's a somewhat verbose, but general approach:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="taxType" select="11" />

  <xsl:template match="transactions">
    <result>
      <xsl:variable name="allValues" 
                    select="transaction/@*[substring(local-name(), 5, 5) = 'Type']
                                          [. = $taxType]" />
      <xsl:call-template name="SumValues">
        <xsl:with-param name="items" select="$allValues" />
      </xsl:call-template>
    </result>
  </xsl:template>

  <xsl:template name="SumValues">
    <xsl:param name="items" />
    <xsl:param name="total" select="0" />

    <xsl:choose>
      <xsl:when test="not($items)">
        <xsl:value-of select="$total" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="currentItem" select="$items[1]" />
        <xsl:variable name="currentValue">
          <xsl:apply-templates select="$currentItem" mode="getMatchingValue" />
        </xsl:variable>

        <xsl:call-template name="SumValues">
          <xsl:with-param name="items" select="$items[position() > 1]" />
          <xsl:with-param name="total" select="$total +  $currentValue" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*" mode="getMatchingValue">
    <xsl:variable name="typeCodeLetter" select="substring(local-name(), 4, 1)" />
    <xsl:variable name="valueAttributeName"
                  select="concat('Tax', $typeCodeLetter, 'Value')" />
    <xsl:variable name="matchingValueAttribute"
                  select="../@*[local-name() = $valueAttributeName]" />

    <!-- Multiply by boolean to handle the case where the 
         attribute wasn't found-->
    <xsl:value-of select="$matchingValueAttribute * 
                          boolean($matchingValueAttribute)"/>
  </xsl:template>
</xsl:stylesheet>

This should be able to handle any number of Tax*Types (as long as * is a single character) and the desired type can be passed in as a parameter.

Upvotes: 1

DeadlyDan
DeadlyDan

Reputation: 699

This xml is not in a very format suitable for completing the type of processing you are asking to do. your best option is to use javascript or some other language with XML capabilities (C#, Java,etc etc) to extract the details you need.

with javascript you could find any attribute with Tax*Type=11 and then get the value of the next attribute

Upvotes: 0

kamituel
kamituel

Reputation: 35960

Have a look at the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">

    Sum of TaxA where type = 11:
    <xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue)" />

    Sum of all tax where type = 11:
    <xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue) 
      + sum(transactions/transaction[@TaxBType='11']/@TaxBValue) 
      + sum(transactions/transaction[@TaxCType='11']/@TaxCValue)" />

  </xsl:template>
</xsl:stylesheet>

It computes sum of TaxAValue for nodes with TaxAType = 11 and sum of all Tax?Value for nodes with Tax?Type = 11.

Upvotes: 14

Related Questions