NCollinsTE
NCollinsTE

Reputation: 309

XSLT 2.0 SUM of values where containing node equals a specific value

I have some grouping of Cost data that can either be labeled as HCOST or SCOST in an XML file under the cledger node.

I'm looking for a result of:

Hard Costs:  $600.00
Soft Costs:  $200.00

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<superbills>
    <superbill>
        <invoice>
            <matters>
                <matter>
                    <cost-detail>
                        <costcards>
                            <costcard index="1234">
                                <cledger>HCOST</cledger>
                                <card-values>
                                    <card-value type="billed">
                                        <rate>200.00</rate>
                                        <quantity>1.00</quantity>
                                        <amount>200.0000</amount>
                                    </card-value>
                                </card-values>
                                <costcard index="2345">
                                    <cledger>SCOST</cledger>
                                    <card-values>
                                        <card-value type="billed">
                                            <rate>100.00</rate>
                                            <quantity>1.00</quantity>
                                            <amount>100.0000</amount>
                                        </card-value>
                                    </card-values>
                                </costcard>
                                <costcard index="3456">
                                    <cledger>HCOST</cledger>
                                    <card-values>
                                        <card-value type="billed">
                                            <rate>200.00</rate>
                                            <quantity>1.00</quantity>
                                            <amount>200.0000</amount>
                                        </card-value>
                                    </card-values>
                                </costcard>
                                <costcard index="4567">
                                    <cledger>SCOST</cledger>
                                    <card-values>
                                        <card-value type="billed">
                                            <rate>100.00</rate>
                                            <quantity>1.00</quantity>
                                            <amount>100.0000</amount>
                                        </card-value>
                                    </card-values>
                                </costcard>
                                <costcard index="5678">
                                    <cledger>HCOST</cledger>
                                    <card-values>
                                        <card-value type="billed">
                                            <rate>200.00</rate>
                                            <quantity>1.00</quantity>
                                            <amount>200.0000</amount>
                                        </card-value>
                                    </card-values>
                                </costcard>
                            </costcard>
                        </costcards>
                    </cost-detail>
                </matter>
            </matters>
        </invoice>
    </superbill>
</superbills>

Here is the current XSLT I'm using: (I've only been attempting the "HCOST" portion and am only getting $200.00 as the result, not the $600.00 sum I require)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:functx="http://www.functx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" version="2.0">

    <xsl:template name="cost_sample">
        <w:p>
            <w:r>
                <w:t>Hard Costs =</w:t>
            </w:r>
            <xsl:for-each-group select="/superbills/superbill/invoice/matters/matter/cost-detail/costcards/costcard" group-by="cledger='HCOST'">
                <w:r>
                    <w:t>
                        <xsl:value-of select="format-number(card-values/card-value[@type='billed']/amount, '$#,##0.00;-$#,##0.00')" />
                    </w:t>
                </w:r>
            </xsl:for-each-group>
        </w:p>
        <w:p>
            <w:r>
                <w:t>Soft Costs = </w:t>
            </w:r>
        </w:p>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 860

Answers (2)

Matthew Whited
Matthew Whited

Reputation: 22443

XSLT ...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
                version="2.0">

  <xsl:template match="/">
    <w:p>
      <w:r>
        <w:t>Hard Costs =</w:t>
      </w:r>
      <w:r>
        <w:t>
          <xsl:value-of select="format-number(sum(//costcard[cledger='HCOST']/card-values/card-value[@type='billed']/amount), '$#,##0.00;-$#,##0.00')"/>
        </w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>Soft Costs = </w:t>
      </w:r>
      <w:r>
        <w:t>
          <xsl:value-of select="format-number(sum(//costcard[cledger='SCOST']/card-values/card-value[@type='billed']/amount), '$#,##0.00;-$#,##0.00')"/>
        </w:t>
      </w:r>
    </w:p>
  </xsl:template>
</xsl:stylesheet>

Result... (as a note this is a fragment because no root is declared in the stylesheet)

<w:p xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:r>
    <w:t>Hard Costs =</w:t>
  </w:r>
  <w:r>
    <w:t>$600.00</w:t>
  </w:r>
</w:p>
<w:p xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:r>
    <w:t>Soft Costs = </w:t>
  </w:r>
  <w:r>
    <w:t>$200.00</w:t>
  </w:r>
</w:p>

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167716

If you use XPath, as in format-number(sum(/superbills/superbill/invoice/matters/matter/cost-detail/costcards/costcard[cledger='HCOST']/card-values/card-value[@type='billed']/amount), '$#,##0.00;-$#,##0.00'), then you don't need the grouping.

Upvotes: 2

Related Questions