Microsoft XML 2.5 SDK


 

Formatting of Data Types Using Script

[This is preliminary documentation and subject to change.]

The following data fragment shows a portfolio with elements of various data types supported by Microsoft® Internet Explorer 5 as described in the XML Data Types Reference. XSL provides facilities for leveraging these data types when formatting them for end-user presentation.

<portfolio xmlns="xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <date dt:dt="dateTime">1998-10-13T15:56:00</date>
  <stock>
    <symbol>ACXM</symbol>
    <name>acxiom corp</name>
    <price dt:dt="fixed.14.4">18.875</price>
    <change dt:dt="fixed.14.4">-1.250</change>
    <percent dt:dt="fixed.14.4">-6.21</percent>
    <volume dt:dt="fixed.14.4">0.23</volume>
  </stock>
  ...
</portfolio>

The data-typed values can be formatted through <xsl:eval>, the nodeTypedValue method, and XSL helper functions for formatting numbers, dates, and times. The following templates use script to obtain the typed value of each node and call a formatting function upon it. The three formatting functions shown below are formatDate, formatTime, and formatNumber.

<xsl:template match="date">
  <xsl:eval>formatDate(this.nodeTypedValue, "MMMM dd',' yyyy")</xsl:eval>
  at <xsl:eval>formatTime(this.nodeTypedValue, "hh:mm tt")</xsl:eval>
</xsl:template>
  
<xsl:template match="price | change">
  <xsl:eval>formatNumber(this.nodeTypedValue, "$0.00")</xsl:eval>
</xsl:template>
  
<xsl:template match="percent">
  <xsl:eval>formatNumber(this.nodeTypedValue, "0.0")</xsl:eval>%
</xsl:template>
  
<xsl:template match="volume">
  <xsl:eval>formatNumber(this.nodeTypedValue * 1000000, "#,###,###")</xsl:eval>
</xsl:template>

Note that simple string concatenation does not require scripting, as XSL can perform this function. Accordingly, the word "at" in the date template and the "%" in the percent template can be placed outside the <xsl:eval> element.

Using the nodeTypedValue method is substantially easier than parsing a raw date string into a VT_DATE as shown in the following example.

<xsl:template match="date">
  <xsl:eval>formatDate(new Date(Date.parse(this.text)).getVarDate(), "MMMM dd',' yyyy");</xsl:eval>
  at <xsl:eval>formatTime(new Date(Date.parse(this.text)).getVarDate(), "hh:mm tt")</xsl:eval>
</xsl:template>

Parsing the text as shown in this example allows you to operate on dates in XML files that do not declare data types. In addition, the Date.parse method can be used to read date formats that are not supported as dates by XML data types.