【问题标题】:Subtraction by decimal number十进制数减法
【发布时间】:2019-02-06 13:16:46
【问题描述】:

我有一个根据条件减去金额-1。请任何人帮助。

输入:

          <JD>
      <GP xmlns="">
       I xmlns="">
            <PK>40</PK> 
        <A/> 
        <AMNT>11659650.15</AMNT>
        <B/> 
        <C/> 
       </I>
       <I xmlns="">  
        <PK>50</PK> 
        <A/>
        <AMNT>11659650.15</AMNT>
        <B/> 
        <C/> 
         </I>
        </GP>
            </JD>

尝试使用以下 XSLT 并获得 1.165964915E7 50。

               <xsl:for-each select="JD/mo:GP/I">                     
                     <xsl:if test="PK='40'">
    <xsl:variable name="a" select="AMNT"/>
    <xsl:element name="AMT">
    <xsl:value-of select="$a"/>
    </xsl:element>
    </xsl:if>
    <xsl:if test="PK='50'">
   <xsl:variable name="a" select="AMNT"/>
   <xsl:element name="AMT">
        <xsl:value-of select="$a - 1"/>
    </xsl:element>
    </xsl:if>

【问题讨论】:

    标签: xslt-2.0 xslt-3.0


    【解决方案1】:

    考虑您给定的输入如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
    <JD>
        <GP>
            <I>
                <PK>40</PK>
                <A />
                <AMNT>11659650.15</AMNT>
                <B />
                <C />
            </I>
            <I>
                <PK>50</PK>
                <A />
                <AMNT>11659650.15</AMNT>
                <B />
                <C />
            </I>
        </GP>
    </JD>
    </Root>
    

    XSLT 2.0 中,您可以使用xs:decimal 进行尝试,如下所示:

    <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:template match="/Root">
        <xsl:for-each select="JD/mo:GP/I">
            <xsl:if test="PK='40'">
                <xsl:variable name="a" select="AMNT" />
                <xsl:element name="AMT">
                    <xsl:value-of select="$a" />
                </xsl:element>
            </xsl:if>
            <xsl:if test="PK='50'">
                <xsl:variable name="a" select="AMNT" />
                <xsl:element name="AMT">
                    <xsl:value-of select="xs:decimal($a) - 1" />
                </xsl:element>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    

    XSLT 1.0中,使用format-number()函数:

    <xsl:template match="/Root">
        <xsl:for-each select="JD/mo:GP/I">
            <xsl:if test="PK='40'">
                <xsl:variable name="a" select="AMNT" />
                <xsl:element name="AMT">
                    <xsl:value-of select="$a" />
                </xsl:element>
            </xsl:if>
            <xsl:if test="PK='50'">
                <xsl:variable name="a" select="AMNT" />
                <xsl:element name="AMT">
                    <xsl:value-of select="format-number($a - 1, '0.##')" />
                </xsl:element>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 2012-05-27
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2014-12-22
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多