需要为XSL模板的变量添加逗号

时间:2018-09-17 22:19:59

标签: xml

我正在尝试在从我们的客户信息系统打印的帐单上添加逗号。据我了解,该应用程序从sql server中提取客户的数据并创建xml文件。然后将该xml文件与xsl文件进行格式比较,并创建一个新的xml文件和pdf。

我对xml不太了解,但是据我所知,@ previousread,@ current和@usage是从应用程序的xml流中解释的变量。这三个变量表示以前的仪表读数,当前的仪表读数和消耗量(使用量)。

我的组织希望我添加数千个逗号。截至本文发布时,我们的帐单仅将一堆数字一起发布。我看不出数字的方式有问题,也看不到为什么需要逗号,但是我希望使其具有逗号。

我不是Web开发人员,但是我对sql server有一点经验。我花了大约8个小时来研究这个问题,以为我发现它只是发现pdf没有逗号。

我在网上看到了一些示例,这些示例说明了如何在数字为常数时格式化数字,但每个电表读数的值都不相同,并且我不知道如何对此进行补偿。

我已经发布了原始代码和编辑过的代码。

这是原始代码。

<fo:table-cell text-align="left" padding-left="10pt">
<fo:block border-right-style="solid" border-color="black" border-width="0.2pt">
<xsl:value-of select="@utility"/>
</fo:block>
</fo:table-cell>
<xsl:if test="count(meter/meterread) &gt; 0">
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="descendant::meter/meterread[1]/@previousread"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" border-right-style="solid" border-   color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="descendant::meter/meterread[last()]/@current"/>
</fo:block>
</fo:table-cell>   
<fo:table-cell text-align="center" border-right-style="solid" border-  color="black" border-width="0.2pt">
<fo:block>
<xsl:choose>
<xsl:when test="string-length(meter/meterread/@billingusage) &gt; 0">
<xsl:value-of select="sum(meter/meterread/@billingusage)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="sum(meter/meterread/@usage)"/>
</xsl:otherwise>
</xsl:choose>
</fo:block>
</fo:table-cell> 

这是我编辑的代码。

<fo:table-cell text-align="left" padding-left="10pt">
<fo:block border-right-style="solid" border-color="black" border-width="0.2pt">
<xsl:value-of select="@utility"/>
</fo:block>
</fo:table-cell>
<xsl:if test="count(meter/meterread) &gt; 0">
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="descendant::meter/meterread[1]/@previousread"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="descendant::meter/meterread[last()]/@current"/>
</fo:block>
</fo:table-cell>   
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:choose>
<xsl:when test="string-length(meter/meterread/@billingusage) &gt; 0">
<xsl:value-of select="sum(meter/meterread/@billingusage)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="sum(meter/meterread/@usage)"/>
</xsl:otherwise>
</xsl:choose>
</fo:block>
</fo:table-cell> 

2 个答案:

答案 0 :(得分:0)

您可以使用params={..., 'tokenizerOptions': 'asciiQuotes=true'}函数使用逗号将数字格式化为千位分隔符:只需在任何引用中查找它即可。我不知道您想在哪里使用它,也许在format-number()上的调用周围。

顺便说一句,样式表中有一些奇怪的代码。

sum()

如果<xsl:when test="string-length(meter/meterread/@billingusage) &gt; 0"> <xsl:value-of select="sum(meter/meterread/@billingusage)"/> </xsl:when> 是一个包含多个值的集合,那么将meter/meterread/@billingusage应用于它就没有意义;如果它是单个值,则对它应用string-length()毫无意义。

表达式sum()@previousread@current不引用变量,它们引用源文档中的属性。

8小时的时间足以阅读很多东西,所以令您惊讶的是您没有找到@usage函数。

答案 1 :(得分:0)

我不小心将原始代码发布了两次。这是我编辑时的代码:

<fo:table-row>
<fo:table-cell text-align="left" padding-left="10pt">
<fo:block border-right-style="solid" border-color="black" border-width="0.2pt">
<xsl:value-of select="@utility"/>
</fo:block>
</fo:table-cell>
<xsl:if test="count(meter/meterread) &gt; 0">
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="format-number(descendant::meter/meterread[1]/@previousread, '#,###,###')"/>
</fo:block>
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:value-of select="format-number(descendant::meter/meterread[last()]/@current, '#,###,###')"/>
</fo:block>
</fo:table-cell>   
<fo:table-cell text-align="center" border-right-style="solid" border-color="black" border-width="0.2pt">
<fo:block>
<xsl:choose>
<xsl:when test="string-length(meter/meterread/@billingusage) &gt; 0">
<xsl:value-of select="format-number(sum(meter/meterread/@billingusage), '#,###,###')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(sum(meter/meterread/@usage), '#,###,###')"/>
</xsl:otherwise>
</xsl:choose>
</fo:block>
</fo:table-cell>