user3703852
user3703852

Reputation: 3

XSLT - how to find sum of attribute of one node list where attribute value exists in different node list

I have a problem I am running in to with a XSLT 1.0 stylesheet that I imagine is probably fairly simple but I can't seem to quite wrap my mind around it. So I have XML that looks something like this

<Components>
    <Component Quantity="2.0" MaterialCode="1111"/>
        <DispensingInstructions/>
        <ChargeContainers>
            <ChargeContainer BarcodeValue="def"/>
            <ChargeContainer BarcodeValue="jkl"/>
        </ChargeContainers>
        <Comments/>
        <Transactions/>
        <DispenseContainers>
            <Container BarcodeValue="abc" Quantity="1.0"></Container>
            <Container BarcodeValue="def" Quantity="2.0"></Container>
            <Container BarcodeValue="ghi" Quantity="3.0"></Container>
            <Container BarcodeValue="jkl" Quantity="4.0"></Container>
        </DispenseContainers>
    </Component>
    <Component Quantity="1.0" MaterialCode="12345"/>
        <DispensingInstructions/>
        <ChargeContainers/>
        <Comments/>
        <Transactions/>
        <DispenseContainers/>
    </Component>
</Components>

I am trying to get a sum of the "quantity" attribute of DispenseContainers, but only if the container barcode exists in ChargeContainers. So for this example, I would want a function that returned 6 because ChargeContainers has both a node with barcode "def" and a node with barcode "jkl" and DispenseContainers those have quantities of 2 and 4 which sum to 6.

I had some XSL that looks something like this:

<xsl:for-each select="//Component">
    
    <!--
    Place holder for code to make table row with a bunch of info from nodelists of component
    -->
    
    <td>
        <xsl:variable name="TotalDispensedQuantity" select="sum(DispenseContainers/Container/@Quantity)" />
        <xsl:if test="$TotalDispensedQuantity != number(@Quantity)">
            <xsl:attribute name="style">background-color: red;</xsl:attribute>
        </xsl:if>
    </td>
    
</xsl:for-each>                                                                             

but now I have to cross reference the dispense containers to ensure they exist in charge containers before I add their quantity to the sum. I attempted to add the key as stated in the answers like so:

<xsl:for-each select="//Component">

    <!--Get sum of dispensed containers quantities if they exist in charged containers list-->
    <xsl:key name="k1" match="//ChargeContainers/ChargeContainer" use="@BarcodeValue" />
    <xsl:template match=".">
        <result>
            <xsl:variable name="TotalChargedQuantity" select = "sum(DispenseContainers/Container[key('k1', @BarcodeValue)]/@Quantity)"/>
        </result>
    </xsl:template>
    
    <!--
    Place holder for code to make table row with a bunch of info from nodelists of component
    -->
    
    <td>
        <xsl:if test="$TotalChargeQuantity != number(@Quantity)">
            <xsl:attribute name="style">background-color: red;</xsl:attribute>
        </xsl:if>
    </td>
    
</xsl:for-each>

however, I get an error stating that key can't be a child element of for-each. Is there a...loop friendly way of doing this?

Edit1: I'm going to rewrite the XML to be a bit closer to real as well as give a bit more context. The solution given is probably correct, but I'm getting an error that key can't be a child of for-each

Upvotes: 0

Views: 203

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Use a key to resolve cross-references - for example:

XSLT 1.0

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

<xsl:key name="k1" match="NodeList1/Node" use="@name" />

<xsl:template match="/Root">
    <result>
        <xsl:value-of select="sum(NodeList2/Node[key('k1', @name)]/@value)"/>  
    </result>
</xsl:template>

</xsl:stylesheet>

Added:

Your edited question is so different from the original, it should have been posted as a new one.

In order to limit the action of the key to the current Component node, you need to add a unique identifier of the current Component to the key:

XSLT 1.0

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

<xsl:key name="k1" match="ChargeContainer" use="concat(generate-id(ancestor::Component), '|',  @BarcodeValue)" />

<xsl:template match="/Components">
    <table>
        <tr>
            <th>Material Code</th>
            <th>Your Result</th>
        </tr>
        <xsl:for-each select="Component">
            <xsl:variable name="component-id" select="generate-id()" />
            <tr>
                <td>
                    <xsl:value-of select="@MaterialCode"/>  
                </td>
                <td>
                    <xsl:value-of select="sum(DispenseContainers/Container[key('k1', concat($component-id, '|', @BarcodeValue))]/@Quantity)"/>  
                </td>
            </tr>
        </xsl:for-each>  
    </table>        
</xsl:template>

</xsl:stylesheet>

Demo (using a well-formed (!) XML as the input): https://xsltfiddle.liberty-development.net/nb9PtDR

Upvotes: 1

Related Questions