xsl for-each within for-each-group group-by



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am new to xsl. I understand that some similar questions have been asked, but I need help since I can't figure out what was wrong with my code. I have xml transactions file. I want to ,first, group by date, then sort each group of transactions by time



I have this xml



<History>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000001</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000487</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000003</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000494</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000002</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000489</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000004</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000486</Balance>
<Description />
</ShareLoanResults>
</History>


I am looking for this result



<History>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<Amount>4</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<Amount>1</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>2</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
</History>


Here is my xsl



<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" />

<xsl:template match="/">
<xsl:copy>
<xsl:for-each-group select="History/ShareLoanResults" group-by="PostingDt">
<xsl:for-each select="current-group()">
<xsl:sort select="PostingTime" order="ascending"/>
<ShareLoanResults>
<Order>
<xsl:value-of select="position()"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</ShareLoanResults>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


However, I am getting exception: javax.xml.transform.TransformerException: Could not find function: current-group



If I remove the <xsl:for-each-group select="current-group()"> and its corresponding closed tag, I get empty result below



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShareLoanResults><Order/><Account/><PostingDt/><PostingTime/><Amount>NaN</Amount></ShareLoanResults>


Could someone please tell me what I did wrong and/or was missing?
Thanks!










share|improve this question



















  • 1





    Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

    – michael.hor257k
    Nov 15 '18 at 18:27






  • 1





    See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

    – Daniel Haley
    Nov 15 '18 at 18:29







  • 2





    I don't see any actual grouping done here.

    – michael.hor257k
    Nov 15 '18 at 18:33






  • 1





    Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

    – Daniel Haley
    Nov 15 '18 at 18:34











  • @DanielHaley Well, there is a running number within each group.

    – michael.hor257k
    Nov 15 '18 at 18:36


















1















I am new to xsl. I understand that some similar questions have been asked, but I need help since I can't figure out what was wrong with my code. I have xml transactions file. I want to ,first, group by date, then sort each group of transactions by time



I have this xml



<History>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000001</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000487</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000003</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000494</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000002</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000489</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000004</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000486</Balance>
<Description />
</ShareLoanResults>
</History>


I am looking for this result



<History>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<Amount>4</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<Amount>1</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>2</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
</History>


Here is my xsl



<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" />

<xsl:template match="/">
<xsl:copy>
<xsl:for-each-group select="History/ShareLoanResults" group-by="PostingDt">
<xsl:for-each select="current-group()">
<xsl:sort select="PostingTime" order="ascending"/>
<ShareLoanResults>
<Order>
<xsl:value-of select="position()"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</ShareLoanResults>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


However, I am getting exception: javax.xml.transform.TransformerException: Could not find function: current-group



If I remove the <xsl:for-each-group select="current-group()"> and its corresponding closed tag, I get empty result below



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShareLoanResults><Order/><Account/><PostingDt/><PostingTime/><Amount>NaN</Amount></ShareLoanResults>


Could someone please tell me what I did wrong and/or was missing?
Thanks!










share|improve this question



















  • 1





    Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

    – michael.hor257k
    Nov 15 '18 at 18:27






  • 1





    See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

    – Daniel Haley
    Nov 15 '18 at 18:29







  • 2





    I don't see any actual grouping done here.

    – michael.hor257k
    Nov 15 '18 at 18:33






  • 1





    Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

    – Daniel Haley
    Nov 15 '18 at 18:34











  • @DanielHaley Well, there is a running number within each group.

    – michael.hor257k
    Nov 15 '18 at 18:36














1












1








1








I am new to xsl. I understand that some similar questions have been asked, but I need help since I can't figure out what was wrong with my code. I have xml transactions file. I want to ,first, group by date, then sort each group of transactions by time



I have this xml



<History>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000001</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000487</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000003</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000494</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000002</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000489</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000004</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000486</Balance>
<Description />
</ShareLoanResults>
</History>


I am looking for this result



<History>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<Amount>4</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<Amount>1</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>2</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
</History>


Here is my xsl



<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" />

<xsl:template match="/">
<xsl:copy>
<xsl:for-each-group select="History/ShareLoanResults" group-by="PostingDt">
<xsl:for-each select="current-group()">
<xsl:sort select="PostingTime" order="ascending"/>
<ShareLoanResults>
<Order>
<xsl:value-of select="position()"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</ShareLoanResults>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


However, I am getting exception: javax.xml.transform.TransformerException: Could not find function: current-group



If I remove the <xsl:for-each-group select="current-group()"> and its corresponding closed tag, I get empty result below



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShareLoanResults><Order/><Account/><PostingDt/><PostingTime/><Amount>NaN</Amount></ShareLoanResults>


Could someone please tell me what I did wrong and/or was missing?
Thanks!










share|improve this question
















I am new to xsl. I understand that some similar questions have been asked, but I need help since I can't figure out what was wrong with my code. I have xml transactions file. I want to ,first, group by date, then sort each group of transactions by time



I have this xml



<History>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000001</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000487</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000003</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000494</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000002</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000489</Balance>
<Description />
</ShareLoanResults>
<ShareLoanResults>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<AmtLoan>000000000</AmtLoan>
<AmtShare>000000004</AmtShare>
<AmtInt>000000000</AmtInt>
<Balance>000000486</Balance>
<Description />
</ShareLoanResults>
</History>


I am looking for this result



<History>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180717</PostingDt>
<PostingTime>215641</PostingTime>
<Amount>4</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180718</PostingDt>
<PostingTime>215642</PostingTime>
<Amount>1</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>1</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215640</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
<ShareLoanResults>
<Order>2</Order>
<Account>123</Account>
<PostingDt>20180719</PostingDt>
<PostingTime>215650</PostingTime>
<Amount>3</Amount>
</ShareLoanResults>
</History>


Here is my xsl



<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" />

<xsl:template match="/">
<xsl:copy>
<xsl:for-each-group select="History/ShareLoanResults" group-by="PostingDt">
<xsl:for-each select="current-group()">
<xsl:sort select="PostingTime" order="ascending"/>
<ShareLoanResults>
<Order>
<xsl:value-of select="position()"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</ShareLoanResults>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


However, I am getting exception: javax.xml.transform.TransformerException: Could not find function: current-group



If I remove the <xsl:for-each-group select="current-group()"> and its corresponding closed tag, I get empty result below



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShareLoanResults><Order/><Account/><PostingDt/><PostingTime/><Amount>NaN</Amount></ShareLoanResults>


Could someone please tell me what I did wrong and/or was missing?
Thanks!







xml xslt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:22









Daniel Haley

40.1k45581




40.1k45581










asked Nov 15 '18 at 17:28









HenryHenry

82




82







  • 1





    Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

    – michael.hor257k
    Nov 15 '18 at 18:27






  • 1





    See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

    – Daniel Haley
    Nov 15 '18 at 18:29







  • 2





    I don't see any actual grouping done here.

    – michael.hor257k
    Nov 15 '18 at 18:33






  • 1





    Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

    – Daniel Haley
    Nov 15 '18 at 18:34











  • @DanielHaley Well, there is a running number within each group.

    – michael.hor257k
    Nov 15 '18 at 18:36













  • 1





    Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

    – michael.hor257k
    Nov 15 '18 at 18:27






  • 1





    See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

    – Daniel Haley
    Nov 15 '18 at 18:29







  • 2





    I don't see any actual grouping done here.

    – michael.hor257k
    Nov 15 '18 at 18:33






  • 1





    Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

    – Daniel Haley
    Nov 15 '18 at 18:34











  • @DanielHaley Well, there is a running number within each group.

    – michael.hor257k
    Nov 15 '18 at 18:36








1




1





Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

– michael.hor257k
Nov 15 '18 at 18:27





Looks like you're trying to run an XSLT 2.0 stylesheet on an XSLT 1.0 processor.

– michael.hor257k
Nov 15 '18 at 18:27




1




1





See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

– Daniel Haley
Nov 15 '18 at 18:29






See stackoverflow.com/a/25245033/317052 for a way to check what version your processor supports. If it only supports 1.0, you'll either need to change to a 2.0 processor or use muenchian grouping in a 1.0 stylesheet.

– Daniel Haley
Nov 15 '18 at 18:29





2




2





I don't see any actual grouping done here.

– michael.hor257k
Nov 15 '18 at 18:33





I don't see any actual grouping done here.

– michael.hor257k
Nov 15 '18 at 18:33




1




1





Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

– Daniel Haley
Nov 15 '18 at 18:34





Just noticed the same thing @michael.hor257k. This seems like just a sorting issue.

– Daniel Haley
Nov 15 '18 at 18:34













@DanielHaley Well, there is a running number within each group.

– michael.hor257k
Nov 15 '18 at 18:36






@DanielHaley Well, there is a running number within each group.

– michael.hor257k
Nov 15 '18 at 18:36













1 Answer
1






active

oldest

votes


















1














As mentioned by our XSLT gurus, you do not need any grouping or even a 2.0 processor. Simply sort by date and time, and have Order node count following siblings with same date:



<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/History">
<xsl:copy>
<xsl:apply-templates select="ShareLoanResults">
<xsl:sort select="PostingDt"/>
<xsl:sort select="PostingTime"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="ShareLoanResults">
<xsl:variable name="curr_dt" select="PostingDt"/>
<xsl:copy>
<Order>
<xsl:value-of select="count(following-sibling::*[PostingDt=$curr_dt])+1"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


XSLT Demo






share|improve this answer

























  • Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

    – Henry
    Nov 15 '18 at 20:56






  • 1





    @Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

    – Martin Honnen
    Nov 15 '18 at 21:18











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%2f53324924%2fxsl-for-each-within-for-each-group-group-by%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









1














As mentioned by our XSLT gurus, you do not need any grouping or even a 2.0 processor. Simply sort by date and time, and have Order node count following siblings with same date:



<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/History">
<xsl:copy>
<xsl:apply-templates select="ShareLoanResults">
<xsl:sort select="PostingDt"/>
<xsl:sort select="PostingTime"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="ShareLoanResults">
<xsl:variable name="curr_dt" select="PostingDt"/>
<xsl:copy>
<Order>
<xsl:value-of select="count(following-sibling::*[PostingDt=$curr_dt])+1"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


XSLT Demo






share|improve this answer

























  • Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

    – Henry
    Nov 15 '18 at 20:56






  • 1





    @Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

    – Martin Honnen
    Nov 15 '18 at 21:18















1














As mentioned by our XSLT gurus, you do not need any grouping or even a 2.0 processor. Simply sort by date and time, and have Order node count following siblings with same date:



<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/History">
<xsl:copy>
<xsl:apply-templates select="ShareLoanResults">
<xsl:sort select="PostingDt"/>
<xsl:sort select="PostingTime"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="ShareLoanResults">
<xsl:variable name="curr_dt" select="PostingDt"/>
<xsl:copy>
<Order>
<xsl:value-of select="count(following-sibling::*[PostingDt=$curr_dt])+1"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


XSLT Demo






share|improve this answer

























  • Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

    – Henry
    Nov 15 '18 at 20:56






  • 1





    @Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

    – Martin Honnen
    Nov 15 '18 at 21:18













1












1








1







As mentioned by our XSLT gurus, you do not need any grouping or even a 2.0 processor. Simply sort by date and time, and have Order node count following siblings with same date:



<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/History">
<xsl:copy>
<xsl:apply-templates select="ShareLoanResults">
<xsl:sort select="PostingDt"/>
<xsl:sort select="PostingTime"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="ShareLoanResults">
<xsl:variable name="curr_dt" select="PostingDt"/>
<xsl:copy>
<Order>
<xsl:value-of select="count(following-sibling::*[PostingDt=$curr_dt])+1"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


XSLT Demo






share|improve this answer















As mentioned by our XSLT gurus, you do not need any grouping or even a 2.0 processor. Simply sort by date and time, and have Order node count following siblings with same date:



<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/History">
<xsl:copy>
<xsl:apply-templates select="ShareLoanResults">
<xsl:sort select="PostingDt"/>
<xsl:sort select="PostingTime"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="ShareLoanResults">
<xsl:variable name="curr_dt" select="PostingDt"/>
<xsl:copy>
<Order>
<xsl:value-of select="count(following-sibling::*[PostingDt=$curr_dt])+1"/>
</Order>
<Account>
<xsl:value-of select="Account"/>
</Account>
<PostingDt>
<xsl:value-of select="PostingDt"/>
</PostingDt>
<PostingTime>
<xsl:value-of select="PostingTime"/>
</PostingTime>
<Amount>
<xsl:value-of select="format-number(AmtShare,'#.000000')"/>
</Amount>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


XSLT Demo







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 21:41

























answered Nov 15 '18 at 19:46









ParfaitParfait

54.5k104872




54.5k104872












  • Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

    – Henry
    Nov 15 '18 at 20:56






  • 1





    @Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

    – Martin Honnen
    Nov 15 '18 at 21:18

















  • Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

    – Henry
    Nov 15 '18 at 20:56






  • 1





    @Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

    – Martin Honnen
    Nov 15 '18 at 21:18
















Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

– Henry
Nov 15 '18 at 20:56





Thank you Parfait!!! According to xsltfiddle.liberty-development.net, your solution is correct. However, when I try to to use it in java xsl transformer, it gives me the exception "Can't have more than one root on a DOM!". By chance, do you know what the cause is? Thanks!!!

– Henry
Nov 15 '18 at 20:56




1




1





@Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

– Martin Honnen
Nov 15 '18 at 21:18





@Henry, change the template for History to wrap the xsl:apply-templates into an xsl:copy, as done in xsltfiddle.liberty-development.net/nc4NzRp/10, that way you get a result with a single History root element containing those ShareLoanResults elements. Or make sure on the Java side you use a DOMResult over a document fragment node, not a document node.

– Martin Honnen
Nov 15 '18 at 21:18



















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%2f53324924%2fxsl-for-each-within-for-each-group-group-by%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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

JS Script not recognized in ASP.Net Page

Syphilis