循环在 XSLT 中创建 N 个等额付款

发布于 2025-01-09 00:57:59 字数 4690 浏览 0 评论 0原文

我对这门语言还很陌生,需要在 xslt 中创建循环来获得帮助。我有一个包含以下信息的 XML:

 <Number_of_Payments>12</Number_of_Payments>
 <First_Payment_Date>28-10-2020</First_Payment_Date>
 <Monthly_Payment>760,00</Monthly_Payment>

我需要将其转换为定期每月付款(12 次付款,金额为 760)。到目前为止,我在 xslt 中进行了第一笔付款:

<paymentplan>
    <loanpaymentplantype>Flat</loanpaymentplantype>
    <intervaltype>StartOfMonth</intervaltype>
    <interval>1</interval>
    <startdate>
      <xsl:call-template name="formatdate">
                  <xsl:with-param name="DateTimeStr" select="Account/First_Payment_Date"/>
      </xsl:call-template>
    </startdate>
    <maturitydate>
      <xsl:call-template name="formatdate">
        <xsl:with-param name="DateTimeStr" select="Account/Last_Payment_Date"/>
    </xsl:call-template>
    </maturitydate>
    <numpayments>
      <xsl:value-of select="Account/Number_of_Payments"/>
    </numpayments>
    <payments>
      <payment>
          <paymentdate>
              <xsl:call-template name="formatdate">
                  <xsl:with-param name="DateTimeStr" select="Account/First_Payment_Date"/>
              </xsl:call-template>
              <!--<xsl:value-of select="concat(substring(First_Payment_Date, 7, 4),'-',substring(First_Payment_Date, 4, 2),'-',substring(First_Payment_Date, 1, 2))"/>-->
          </paymentdate>
          <amount>
              <xsl:value-of select="translate(Account/Monthly_Payment, ',', '.')"/>
          </amount>
          <principalamount>
              <xsl:value-of select="translate(Account/Monthly_Payment, ',', '.')"/>
          </principalamount>
          <fixedinterest>0</fixedinterest>
      </payment>
    </payments>

这是我得到的输出:

<paymentplan>
            <loanpaymentplantype>Flat</loanpaymentplantype>
            <intervaltype>StartOfMonth</intervaltype>
            <interval>1</interval>
            <startdate>2020-11-01</startdate>
            <maturitydate>2021-10-01</maturitydate>
            <numpayments>12</numpayments>
            <payments>
               <payment>
                  <paymentdate>2020-11-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
            </payments>
         </paymentplan>

我需要的是付款计划中的 12 笔付款:

<payment>
                  <paymentdate>2020-11-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2020-12-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-01-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-02-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-03-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-04-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>

等等...

I am pretty new in this language and need help by creating a loop in xslt. I have an XML with info below:

 <Number_of_Payments>12</Number_of_Payments>
 <First_Payment_Date>28-10-2020</First_Payment_Date>
 <Monthly_Payment>760,00</Monthly_Payment>

And I need to transform this to a recurrent monthly payment (12 payments of 760). So far I have the first payment in xslt:

<paymentplan>
    <loanpaymentplantype>Flat</loanpaymentplantype>
    <intervaltype>StartOfMonth</intervaltype>
    <interval>1</interval>
    <startdate>
      <xsl:call-template name="formatdate">
                  <xsl:with-param name="DateTimeStr" select="Account/First_Payment_Date"/>
      </xsl:call-template>
    </startdate>
    <maturitydate>
      <xsl:call-template name="formatdate">
        <xsl:with-param name="DateTimeStr" select="Account/Last_Payment_Date"/>
    </xsl:call-template>
    </maturitydate>
    <numpayments>
      <xsl:value-of select="Account/Number_of_Payments"/>
    </numpayments>
    <payments>
      <payment>
          <paymentdate>
              <xsl:call-template name="formatdate">
                  <xsl:with-param name="DateTimeStr" select="Account/First_Payment_Date"/>
              </xsl:call-template>
              <!--<xsl:value-of select="concat(substring(First_Payment_Date, 7, 4),'-',substring(First_Payment_Date, 4, 2),'-',substring(First_Payment_Date, 1, 2))"/>-->
          </paymentdate>
          <amount>
              <xsl:value-of select="translate(Account/Monthly_Payment, ',', '.')"/>
          </amount>
          <principalamount>
              <xsl:value-of select="translate(Account/Monthly_Payment, ',', '.')"/>
          </principalamount>
          <fixedinterest>0</fixedinterest>
      </payment>
    </payments>

This is the Output that I get:

<paymentplan>
            <loanpaymentplantype>Flat</loanpaymentplantype>
            <intervaltype>StartOfMonth</intervaltype>
            <interval>1</interval>
            <startdate>2020-11-01</startdate>
            <maturitydate>2021-10-01</maturitydate>
            <numpayments>12</numpayments>
            <payments>
               <payment>
                  <paymentdate>2020-11-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
            </payments>
         </paymentplan>

What I would need is 12 payments on the paymentplan:

<payment>
                  <paymentdate>2020-11-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2020-12-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-01-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-02-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-03-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>
<payment>
                  <paymentdate>2021-04-01</paymentdate>
                  <amount>760.00</amount>
                  <principalamount>760.00</principalamount>
                  <fixedinterest>0</fixedinterest>
               </payment>

And so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2025-01-16 00:57:59

您可以使用以下内容作为起点:

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:template match="/">
    <payments>
        <xsl:call-template name="generate-payments">
            <xsl:with-param name="start-year" select="2020"/>
            <xsl:with-param name="start-month" select="11"/>
            <xsl:with-param name="number-of-payments" select="12"/>
            <xsl:with-param name="amount" select="760"/>
        </xsl:call-template>
    </payments>
</xsl:template>

<xsl:template name="generate-payments">
    <xsl:param name="start-year"/>
    <xsl:param name="start-month"/>
    <xsl:param name="number-of-payments"/>
    <xsl:param name="amount"/>
    <xsl:if test="$number-of-payments > 0">
        <payment>
            <paymentdate>
                <xsl:value-of select="$start-year"/>
                <xsl:value-of select="format-number($start-month, '-00')"/>
                <xsl:text>-01</xsl:text>
            </paymentdate>
            <amount>
                <xsl:value-of select="format-number($amount, '#,##0.00')"/>
            </amount>
        </payment>
        <!-- recursive call -->
        <xsl:variable name="m" select="12*$start-year + $start-month" />
        <xsl:call-template name="generate-payments">
            <xsl:with-param name="start-year" select="floor($m div 12)"/>
            <xsl:with-param name="start-month" select="$m mod 12 + 1"/>
            <xsl:with-param name="number-of-payments" select="$number-of-payments - 1"/>
            <xsl:with-param name="amount" select="$amount"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<payments>
   <payment>
      <paymentdate>2020-11-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2020-12-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-01-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-02-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-03-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-04-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-05-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-06-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-07-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-08-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-09-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-10-01</paymentdate>
      <amount>760.00</amount>
   </payment>
</payments>

Here is something you could use as your starting point:

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:template match="/">
    <payments>
        <xsl:call-template name="generate-payments">
            <xsl:with-param name="start-year" select="2020"/>
            <xsl:with-param name="start-month" select="11"/>
            <xsl:with-param name="number-of-payments" select="12"/>
            <xsl:with-param name="amount" select="760"/>
        </xsl:call-template>
    </payments>
</xsl:template>

<xsl:template name="generate-payments">
    <xsl:param name="start-year"/>
    <xsl:param name="start-month"/>
    <xsl:param name="number-of-payments"/>
    <xsl:param name="amount"/>
    <xsl:if test="$number-of-payments > 0">
        <payment>
            <paymentdate>
                <xsl:value-of select="$start-year"/>
                <xsl:value-of select="format-number($start-month, '-00')"/>
                <xsl:text>-01</xsl:text>
            </paymentdate>
            <amount>
                <xsl:value-of select="format-number($amount, '#,##0.00')"/>
            </amount>
        </payment>
        <!-- recursive call -->
        <xsl:variable name="m" select="12*$start-year + $start-month" />
        <xsl:call-template name="generate-payments">
            <xsl:with-param name="start-year" select="floor($m div 12)"/>
            <xsl:with-param name="start-month" select="$m mod 12 + 1"/>
            <xsl:with-param name="number-of-payments" select="$number-of-payments - 1"/>
            <xsl:with-param name="amount" select="$amount"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<payments>
   <payment>
      <paymentdate>2020-11-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2020-12-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-01-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-02-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-03-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-04-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-05-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-06-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-07-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-08-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-09-01</paymentdate>
      <amount>760.00</amount>
   </payment>
   <payment>
      <paymentdate>2021-10-01</paymentdate>
      <amount>760.00</amount>
   </payment>
</payments>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文