我是xsl的新手。我知道有人问过类似的问题,但由于我无法弄清楚代码出了什么问题,因此需要帮助。我有xml交易文件。我想先按日期分组,然后按时间对每组交易进行排序
我有这个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>
我正在寻找这个结果
<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>
这是我的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>
但是,我遇到异常:javax.xml.transform.TransformerException:找不到函数:current-group
如果我删除了<xsl:for-each-group select="current-group()">
及其相应的关闭标签,则我在下面得到空结果
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ShareLoanResults><Order/><Account/><PostingDt/><PostingTime/><Amount>NaN</Amount></ShareLoanResults>
有人可以告诉我我做错了什么和/或丢失了什么吗? 谢谢!
答案 0 :(得分:1)
正如我们的XSLT专家所提到的,您不需要任何分组,甚至不需要2.0处理器。只需按日期和时间排序,并在具有相同日期的兄弟姐妹中计算 Order 个节点:
<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>