Sum the count of 3 nested for-each in XSLT












1















I have the following XSLT:



<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.3ds.com/xsd/XPDMXML">
<xsl:output method="text" doctype-public="XSLT-compat" encoding="ISO-8859-1"/>
<xsl:template match="/">
<xsl:for-each select="/foo/bar/AAA[Owned/text()='1']">
<xsl:variable name="vOP">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/BBB[Owned[text()=$vOP]]">
<xsl:variable name="vTO">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/CCC[Owned[text()=$vTO]]">
<xsl:variable name="vIE">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:text>"COUNT": </xsl:text><xsl:value-of select="count(/foo/buzz/DDD[Owned[text()=$vIE]])"/><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This is a sample input



<?xml version='1.0' encoding='utf-8'?>
<foo>
<bar>
<AAA>
<Owned>1</Owned>
<Instancing>2</Instancing>
</AAA>
<BBB>
<Owned>2</Owned>
<Instancing>3</Instancing>
</BBB>
<CCC>
<Owned>3</Owned>
<Instancing>4</Instancing>
</CCC>
<CCC>
<Owned>3</Owned>
<Instancing>5</Instancing>
</CCC>
<CCC><Owned>4</Owned></CCC>
</bar>
<buzz>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>5</Owned></DDD>
<DDD><Owned>3</Owned></DDD>
<CCC><Owned>4</Owned></CCC>
</buzz>
</foo>


Is there a way to get the total value (SUM) of the latest value-of call? And possibly remove all the foreach ?



The output with that should be 3 (2 + 1).










share|improve this question




















  • 1





    Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

    – Martin Honnen
    Nov 23 '18 at 12:52











  • @MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

    – Thomas Ruiz
    Nov 23 '18 at 13:40











  • It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

    – Michael Kay
    Nov 23 '18 at 17:51
















1















I have the following XSLT:



<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.3ds.com/xsd/XPDMXML">
<xsl:output method="text" doctype-public="XSLT-compat" encoding="ISO-8859-1"/>
<xsl:template match="/">
<xsl:for-each select="/foo/bar/AAA[Owned/text()='1']">
<xsl:variable name="vOP">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/BBB[Owned[text()=$vOP]]">
<xsl:variable name="vTO">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/CCC[Owned[text()=$vTO]]">
<xsl:variable name="vIE">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:text>"COUNT": </xsl:text><xsl:value-of select="count(/foo/buzz/DDD[Owned[text()=$vIE]])"/><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This is a sample input



<?xml version='1.0' encoding='utf-8'?>
<foo>
<bar>
<AAA>
<Owned>1</Owned>
<Instancing>2</Instancing>
</AAA>
<BBB>
<Owned>2</Owned>
<Instancing>3</Instancing>
</BBB>
<CCC>
<Owned>3</Owned>
<Instancing>4</Instancing>
</CCC>
<CCC>
<Owned>3</Owned>
<Instancing>5</Instancing>
</CCC>
<CCC><Owned>4</Owned></CCC>
</bar>
<buzz>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>5</Owned></DDD>
<DDD><Owned>3</Owned></DDD>
<CCC><Owned>4</Owned></CCC>
</buzz>
</foo>


Is there a way to get the total value (SUM) of the latest value-of call? And possibly remove all the foreach ?



The output with that should be 3 (2 + 1).










share|improve this question




















  • 1





    Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

    – Martin Honnen
    Nov 23 '18 at 12:52











  • @MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

    – Thomas Ruiz
    Nov 23 '18 at 13:40











  • It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

    – Michael Kay
    Nov 23 '18 at 17:51














1












1








1








I have the following XSLT:



<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.3ds.com/xsd/XPDMXML">
<xsl:output method="text" doctype-public="XSLT-compat" encoding="ISO-8859-1"/>
<xsl:template match="/">
<xsl:for-each select="/foo/bar/AAA[Owned/text()='1']">
<xsl:variable name="vOP">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/BBB[Owned[text()=$vOP]]">
<xsl:variable name="vTO">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/CCC[Owned[text()=$vTO]]">
<xsl:variable name="vIE">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:text>"COUNT": </xsl:text><xsl:value-of select="count(/foo/buzz/DDD[Owned[text()=$vIE]])"/><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This is a sample input



<?xml version='1.0' encoding='utf-8'?>
<foo>
<bar>
<AAA>
<Owned>1</Owned>
<Instancing>2</Instancing>
</AAA>
<BBB>
<Owned>2</Owned>
<Instancing>3</Instancing>
</BBB>
<CCC>
<Owned>3</Owned>
<Instancing>4</Instancing>
</CCC>
<CCC>
<Owned>3</Owned>
<Instancing>5</Instancing>
</CCC>
<CCC><Owned>4</Owned></CCC>
</bar>
<buzz>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>5</Owned></DDD>
<DDD><Owned>3</Owned></DDD>
<CCC><Owned>4</Owned></CCC>
</buzz>
</foo>


Is there a way to get the total value (SUM) of the latest value-of call? And possibly remove all the foreach ?



The output with that should be 3 (2 + 1).










share|improve this question
















I have the following XSLT:



<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.3ds.com/xsd/XPDMXML">
<xsl:output method="text" doctype-public="XSLT-compat" encoding="ISO-8859-1"/>
<xsl:template match="/">
<xsl:for-each select="/foo/bar/AAA[Owned/text()='1']">
<xsl:variable name="vOP">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/BBB[Owned[text()=$vOP]]">
<xsl:variable name="vTO">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:for-each select="/foo/bar/CCC[Owned[text()=$vTO]]">
<xsl:variable name="vIE">
<xsl:value-of select="./Instancing"/>
</xsl:variable>
<xsl:text>"COUNT": </xsl:text><xsl:value-of select="count(/foo/buzz/DDD[Owned[text()=$vIE]])"/><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


This is a sample input



<?xml version='1.0' encoding='utf-8'?>
<foo>
<bar>
<AAA>
<Owned>1</Owned>
<Instancing>2</Instancing>
</AAA>
<BBB>
<Owned>2</Owned>
<Instancing>3</Instancing>
</BBB>
<CCC>
<Owned>3</Owned>
<Instancing>4</Instancing>
</CCC>
<CCC>
<Owned>3</Owned>
<Instancing>5</Instancing>
</CCC>
<CCC><Owned>4</Owned></CCC>
</bar>
<buzz>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>4</Owned></DDD>
<DDD><Owned>5</Owned></DDD>
<DDD><Owned>3</Owned></DDD>
<CCC><Owned>4</Owned></CCC>
</buzz>
</foo>


Is there a way to get the total value (SUM) of the latest value-of call? And possibly remove all the foreach ?



The output with that should be 3 (2 + 1).







xslt xslt-1.0 xslt-2.0 xslt-grouping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:38







Thomas Ruiz

















asked Nov 23 '18 at 11:46









Thomas RuizThomas Ruiz

3,17121530




3,17121530








  • 1





    Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

    – Martin Honnen
    Nov 23 '18 at 12:52











  • @MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

    – Thomas Ruiz
    Nov 23 '18 at 13:40











  • It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

    – Michael Kay
    Nov 23 '18 at 17:51














  • 1





    Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

    – Martin Honnen
    Nov 23 '18 at 12:52











  • @MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

    – Thomas Ruiz
    Nov 23 '18 at 13:40











  • It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

    – Michael Kay
    Nov 23 '18 at 17:51








1




1





Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

– Martin Honnen
Nov 23 '18 at 12:52





Can you show us a relevant minimal input sample and the corresponding result you get now plus the one you want? It seems you want to group items by Owned but don't use xsl:for-each-group. Not sure why. But in any case, if you want to accumulate values you can use sum on an adequately constructed sequence, so I guess you want to store the result of your grouping in a variable, probably with an XML structure, and then you can use sum on the relevant values you have in the variable.

– Martin Honnen
Nov 23 '18 at 12:52













@MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

– Thomas Ruiz
Nov 23 '18 at 13:40





@MartinHonnen I added some input to work with, and an expected output. I know the very basics of XSL, I'll look into for-each-group.

– Thomas Ruiz
Nov 23 '18 at 13:40













It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

– Michael Kay
Nov 23 '18 at 17:51





It's not the cause of your problem, but writing <xsl:variable name="vTO"><xsl:value-of select="./Instancing"/></xsl:variable> is verbose and inefficient when you could write <xsl:variable name="vTO" select="Instancing"/>

– Michael Kay
Nov 23 '18 at 17:51












1 Answer
1






active

oldest

votes


















2














You can store the result of the first computation in a variable and then sum up values from the variable (and output them:



<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="/foo/bar/AAA[Owned = 1]">
<xsl:variable name="vOP" select="Instancing"/>
<xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
<xsl:variable name="vTO" select="Instancing"/>
<xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
<xsl:variable name="vIE" select="Instancing"/>
<count>
<xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
</count>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>


As for doing it with more compact code, you can use keys to follow cross-references:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:key name="ref" match="bar/*" use="Owned"/>

<xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
<count>
<xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/nc4NzRs/4



I don't know how variable your input data is, perhaps you will need different keys for the different child elements of bar, for your sample data the single key suffices.






share|improve this answer


























  • These solutions work flawlessly! Thank you so much!

    – Thomas Ruiz
    Nov 23 '18 at 14:53












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446124%2fsum-the-count-of-3-nested-for-each-in-xslt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can store the result of the first computation in a variable and then sum up values from the variable (and output them:



<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="/foo/bar/AAA[Owned = 1]">
<xsl:variable name="vOP" select="Instancing"/>
<xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
<xsl:variable name="vTO" select="Instancing"/>
<xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
<xsl:variable name="vIE" select="Instancing"/>
<count>
<xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
</count>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>


As for doing it with more compact code, you can use keys to follow cross-references:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:key name="ref" match="bar/*" use="Owned"/>

<xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
<count>
<xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/nc4NzRs/4



I don't know how variable your input data is, perhaps you will need different keys for the different child elements of bar, for your sample data the single key suffices.






share|improve this answer


























  • These solutions work flawlessly! Thank you so much!

    – Thomas Ruiz
    Nov 23 '18 at 14:53
















2














You can store the result of the first computation in a variable and then sum up values from the variable (and output them:



<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="/foo/bar/AAA[Owned = 1]">
<xsl:variable name="vOP" select="Instancing"/>
<xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
<xsl:variable name="vTO" select="Instancing"/>
<xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
<xsl:variable name="vIE" select="Instancing"/>
<count>
<xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
</count>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>


As for doing it with more compact code, you can use keys to follow cross-references:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:key name="ref" match="bar/*" use="Owned"/>

<xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
<count>
<xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/nc4NzRs/4



I don't know how variable your input data is, perhaps you will need different keys for the different child elements of bar, for your sample data the single key suffices.






share|improve this answer


























  • These solutions work flawlessly! Thank you so much!

    – Thomas Ruiz
    Nov 23 '18 at 14:53














2












2








2







You can store the result of the first computation in a variable and then sum up values from the variable (and output them:



<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="/foo/bar/AAA[Owned = 1]">
<xsl:variable name="vOP" select="Instancing"/>
<xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
<xsl:variable name="vTO" select="Instancing"/>
<xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
<xsl:variable name="vIE" select="Instancing"/>
<count>
<xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
</count>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>


As for doing it with more compact code, you can use keys to follow cross-references:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:key name="ref" match="bar/*" use="Owned"/>

<xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
<count>
<xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/nc4NzRs/4



I don't know how variable your input data is, perhaps you will need different keys for the different child elements of bar, for your sample data the single key suffices.






share|improve this answer















You can store the result of the first computation in a variable and then sum up values from the variable (and output them:



<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="/foo/bar/AAA[Owned = 1]">
<xsl:variable name="vOP" select="Instancing"/>
<xsl:for-each select="/foo/bar/BBB[Owned = $vOP]">
<xsl:variable name="vTO" select="Instancing"/>
<xsl:for-each select="/foo/bar/CCC[Owned = $vTO]">
<xsl:variable name="vIE" select="Instancing"/>
<count>
<xsl:value-of select="count(/foo/buzz/DDD[Owned = $vIE])"/>
</count>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>


As for doing it with more compact code, you can use keys to follow cross-references:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>

<xsl:key name="ref" match="bar/*" use="Owned"/>

<xsl:key name="buzz" match="buzz/DDD" use="Owned"/>

<xsl:template match="/">
<xsl:variable name="counts" as="element(count)*">
<xsl:for-each select="key('ref', key('ref', key('ref', '1')/Instancing)/Instancing)">
<count>
<xsl:value-of select="sum(count(key('buzz', Instancing)))"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$counts/concat('COUNT:', .), concat('SUM:', sum($counts))" separator=","/>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/nc4NzRs/4



I don't know how variable your input data is, perhaps you will need different keys for the different child elements of bar, for your sample data the single key suffices.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 14:41

























answered Nov 23 '18 at 14:15









Martin HonnenMartin Honnen

113k66279




113k66279













  • These solutions work flawlessly! Thank you so much!

    – Thomas Ruiz
    Nov 23 '18 at 14:53



















  • These solutions work flawlessly! Thank you so much!

    – Thomas Ruiz
    Nov 23 '18 at 14:53

















These solutions work flawlessly! Thank you so much!

– Thomas Ruiz
Nov 23 '18 at 14:53





These solutions work flawlessly! Thank you so much!

– Thomas Ruiz
Nov 23 '18 at 14:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446124%2fsum-the-count-of-3-nested-for-each-in-xslt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()