Skip to main content
Inspiring
September 24, 2009
Question

Reading XML - convert values to numeric?

  • September 24, 2009
  • 1 reply
  • 477 views

I'm writing a Cold Fusion page that reads some XML using a Schema.  I've done the validate and the parse and cfdump looks fantastic!

My problem is that I need to operate on the values in the XML as numeric.  It appears that Cold Fusion is ignoring the variable definitions in the schema, and it is reading all XML as text data.  If I do an "isnumeric" I get "NO"; using val() doesn't work because it can't interpret the value as numeric.  How can I read XML integer data as numeric?  It seems like it should be a simple task to convert from character to numeric, I must be missing something easy.

Here's an example of a single value in the XML 'xmlfilename.xml'

<?xml version="1.0" encoding="ISO-8859-1"?>
<thefile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xxx">
<Part_1>
<PartI1_val>199998</PartI1_val>
</Part_1>
</thefile>

Schema definition 'Schema.xsd'
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="thefile">
  <xs:complexType>
   <xs:sequence>

  <xs:element name="Part_1" minOccurs="0">
    <xs:complexType>
     <xs:sequence>
     <xs:element name="PartI1_val" type="xs:integer" nillable="true"  minOccurs="0"/>
     </xs:sequence>
   </xs:complexType>
  </xs:element>

   </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


I'll read this like such in cold fusion:

<cfset xparse = XmlParse('xmlfilename.xml', 'no', 'Schema.xsd')>

if I do this:
<cfdump var="#xparse#">
I see all the data - all values shown as "XmlText".  Looks great!

If I print the value to the web page like such, I see the numeric value:

xparse.thefile.Part_1.PartI1_val=#xparse.thefile.Part_1.PartI1_val#

Looks awesome.

However, I can not val() the result, even if I copy it to another cold fusion variable.  And isnumeric() returns NO.

Thanks if you can help!

    This topic has been closed for replies.

    1 reply

    phammAuthor
    Inspiring
    September 24, 2009

    Looks like all I had to do was add ".xmltext" to my value.  Not specifying ".xmltext" meant that it was resolving to the whole piece of XML complete with tags, which of course can not resolve to a number.   I couldn't see the tags because I was writing my debugging information out to the web page and that meant that they were not being displayed.  Doh!  I knew it had to be simple!