XSLT basics, generic nodes

From what I understand of this code, a generic node is being matched, and an Excel worksheet cell is written with a string type. How do i test this node to see if its a number, so I can put it in a cell with a Numeric type  

<xsl:template match="/*/*/*">

<Cell>

<Data ss:Type="String">

<xsl:value-of select="."/>

</Data>

</Cell>

</xsl:template>

Ideally, I want to say something like

if "/*/*/*" is a number

<cell>

<Data ss:Type="Number"

<xsl:value-of select "."

</data>

</cell>

else

<Cell>

<Data ss:Type="String">

<xsl:value-of select="."/>

</Data>

</Cell>

end if

Once again I've spent hours trying to find this simple thing in example code around the web and found nothing simple...I need this because my conversion is generic, converts any table to excel, but it converts  the number columns in my data to excel columns having a text datatype and a ' (singlequote) gets inserted into the data 



Answer this question

XSLT basics, generic nodes

  • RMD

    Hi Dana,

    Dana Lutz wrote:

    From what I understand of this code, a generic node is being matched, and an Excel worksheet cell is written with a string type. How do i test this node to see if its a number, so I can put it in a cell with a Numeric type

    <xsl:template match="/*/*/*">

    <Cell>

    <Data ss:Type="String">

    <xsl:value-of select="."/>

    </Data>

    </Cell>

    </xsl:template>

    Ideally, I want to say something like

    if "/*/*/*" is a number

    <cell>

    <Data ss:Type="Number"

    <xsl:value-of select "."

    </data>

    </cell>

    else

    <Cell>

    <Data ss:Type="String">

    <xsl:value-of select="."/>

    </Data>

    </Cell>

    end if

    Once again I've spent hours trying to find this simple thing in example code around the web and found nothing simple...I need this because my conversion is generic, converts any table to excel, but it converts the number columns in my data to excel columns having a text datatype and a ' (singlequote) gets inserted into the data

    If the xml document is not accompanied by an xml schema, it cannot be concluded with 100% certainty that the value of a given node is of numeric type.

    Even if we have the following node:

    <x>123.45</x>

    its value may be the number 123.45 but it also may be the string "123.45" and we cannot know which of these two types is the correct one.

    A weaker conclusion -- that the value is castable to numeric value -- is possible. One way to check this is by evaluating the following XPath expression:

    number(node) = number(node)

    The above is evaluated to true() only if the string value of node is castable to a numeric value. If it isn't, then by definition

    number (somethingNotANumber) = NaN

    and again by definition

    NaN is never equal to something, even to NaN

    Hope this helps.

    Cheers,
    Dimitre Novatchev


  • XSLT basics, generic nodes