I'm trying to modify the decimal-format of a stylesheet based on certain information of an XML. More exaclty, I've a XML like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<REPORT>
<LANGUAGE>2</LANGUAGE>
<MYVALUE>123456.78</MYVALUE>
</REPORT>
I'm trying to define the decimal format as european if the language is 2, and default other开发者_运维百科wse. So I've created the following template
<xsl:template match="REPORT">
<xsl:if test="$language=2">
<xsl:decimal-format decimal-separator=',' grouping-separator='.' />
</xsl:if>
<xsl:value-of select ="format-number(MYVALUE,'###.###,00')"/>
</xsl:template>
So it shows the number in european format or in standard format. But I'm getting the following error
xsl:decimal-format is not allowed in this position in the stylesheet!
If I try to put the decimal-format outside the template, then I get the message that the xsl:if is not allowed in this position in the sthylsheet. How can I change the decimal-format based in the XML?
Thanks Jose
The element can only defined directly underneat the element. After definition you then can use the defined formats in the fromat-number function.
<xsl:stylesheet ... namespaces, etc. ...>
<xsl:decimal-format name="de" decimal-separator=',' grouping-separator='.'/>
<xsl:decimal-format name="us" decimal-separator='.' grouping-separator=','/>
<xsl:param name="numFormat">
<xsl:choose>
<xsl:when test="/REPORT/@language = 2">
<xsl:text>us</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>de</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:template match="REPORT">
<xsl:choose>
<xsl:value-of select="format-numer(MYVALUE, '###.###,00', $numFormat)"/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
decimal-format
must be a top-level element, however you can name it and then refer to the name in a conditional construct, perhaps something like the following will work for you.
<xsl:decimal-format name="format1" decimal-separator=',' grouping-separator='.' />
<xsl:template match="REPORT">
<xsl:choose>
<xsl:when test="$language=2">
<xsl:value-of select="format-number(MYVALUE,'###.###,00','format1')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="MYVALUE"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
精彩评论