Xslt 在通过 xsl 文件进行解析时为负值添加括号“( )”作为响应

Xslt Adding brackets '( )' for negative values in response while parsing through xsl file

我正在尝试使用 xslt 从剩余响应生成 pdf 文件。它一切正常,但在负值时,它会在金额值周围添加括号。就像 amount= 500$ 它的工作正常但是对于 amount= -600$ 它给出了 (600$)。

<fo:table-cell>                     
    <fo:block>
    <xsl:value-of select="currency:getFormattedCurrency(amount/currency/text(), amount/amount/text())" />
    </fo:block>
</fo:table-cell>

这是我的 getFormattedCurrency()

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currencyLocale);
        currencyFormatter.setCurrency(currency);
        return currencyFormatter.format(amount);

我还尝试了其他使用 xslt 的 contain() 的方法,这样我就可以识别它是否具有负号字符串,并且我可以区别对待那些负值和正值不同的。像这样--

<fo:table-cell>
    <fo:block>
        <xsl:choose>
            <xsl:when test="contains(string(availableAmount), '-')">
                <xsl:value-of select="-1*(currency:getFormattedCurrency(availableAmount/currency/text(), availableAmount/amount/text()))" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="currency:getFormattedCurrency(availableAmount/currency/text(), availableAmount/amount/text())" />
            </xsl:otherwise>
        </xsl:choose>                           
    </fo:block>                     
</fo:table-cell>

寻找suggestions/solutions最好通过修改xsl文件达到目标结果。附上 是我得到的带有括号的负值和正确的正值的图像,这是实际需要的图片。

很高兴接受解决此问题的其他方法。 :)

因此,您在这里将选择和 java 替换为格式编号。并且,即时创建格式。这有点复杂。看看是不是你想要的。

<fo:table-cell>
  <fo:block>
    <xsl:value-of select="format-number(availableAmount/amount, concat('#,###', availableAmount/currency, ';', '-#,###', availableAmount/currency))" />
  </fo:block>
</fo:table-cell>  

不同的国家有不同的货币格式,所以美国用括号()表示负数,印度用减号表示负数。 XSL 根据国家/地区货币自动格式化此表示法。因此,这实际上不是任何问题,它只是不同货币格式的表示方式。