Link to home
Start Free TrialLog in
Avatar of Miguel Oz
Miguel OzFlag for Australia

asked on

XSLT: Is it possible to assign number format from a variable?

I am converting XML to Excel via XSLT
<xsl:variable name="numberFormat" select="###,##0.00" />
<Styles>
	<Style ss:ID="ShowAsNumber1">
		<NumberFormat ss:Format=numberFormat/>  <= Assign the format here.
	</Style>
</Styles>

Open in new window


If it is not possible what is the  best practice to do that. The xml contains the numbers already formatted but they could be 123.45 or 123.00
By default Excel ignores the .00.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Use attribute value templates{}

      <Style ss:ID="ShowAsNumber1">
            <NumberFormat ss:Format="{$numberFormat}"/>
      </Style>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Miguel Oz

ASKER

Your xslt comments do not seem to work for me. IT gives me "HTTP Error 503. The service is unavailable." (typically means that XSL can not be processed in the Ecrion server)
Note:PFA the detail error If I run XF designer against my XML.
It is weird because if I use the same format string directly in XSLT it works OK.
2016-10-31-XFDesigner-Error.png
Ah well,
<xsl:variable name="numberFormat" select="###,##0.00" />
should be
<xsl:variable name="numberFormat" select="'###,##0.00'" />
(extra single quotes added inside)

the @select does not have a valid XPath, it is looking for an element ###,##0.00, not the string value
Thanks for your help your comment guided me in the right direction.
For anyone looking for the correct answer, my value comes from xml node:
[code]<xsl:variable name="user:numberFormat" select="MyRootNode/NumberFormat/text()"/>[/code]
The code that works is shown below:
[code]
<Style ss:ID="ShowAsNumber">
      <NumberFormat>
            <xsl:attribute name="ss:Format">
                  <xsl:value-of select="$user:numberFormat" disable-output-escaping="yes"/>
            </xsl:attribute>
      </NumberFormat>
</Style>[/code]