Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSAX-SXML and numbers

Tags:

xml

scheme

sxml

I'm using SSAX-SXML for working with a tree structure that just mimics the XML data encoding. So I thought of using the SXML representation directly as the data structure to work with. Everything works very well, and I get all the functionality of default accessors and XPath, which I found very useful.

But I have a problem. XML represents everything as strings, so I need to convert from string to numbers and viceversa, all the time. That will kill performance and just a bad design idea. I was thinking of taking the SXML list and transform all strings to numbers in one pass. But, is there a way that SXML directly does that or any way to tell via XML that something should be represented as a number, not a string?

This is my SXML list:

((wall (@ (uid "2387058723"))
   (pt (@ (y "2.0") (x "1.0")))
   (pt (@ (y "4.0") (x "3.0"))))
(wall (@ (uid "5493820876"))
   (pt (@ (y "0.0") (x "0.0")))
   (pt (@ (y "100.0") (x "0.0")))
   (window (@ (to "0.4") (from "0.2")))
   (window (@ (size "1.0") (from "0.2")))
   (door (@ (size "1.0") (from "0.2"))))
(pilar (@ (uid "692034802"))
    (center (@ (y "5.0") (x "5.0")))
    (dim (@ (b "0.45") (a "0.3"))))
(room (@ (label "salon"))
   (wall (@ (uid "2387058723")))
   (wall (@ (uid "5493820876")))
   (wall (@ (uid "5394501263")))
   (wall (@ (uid "0034923049"))))
(entry (@ (doorNumber "0")) (wall (@ (uid "5493820876"))))
(num "0,9")
(pipe (@ (y "8.0") (x "10.0"))))

From a XML that looks like this (extract):

        <wall uid="2387058723">
            <pt x="1.0" y="2.0"/>
            <pt x="3.0" y="4.0"/>
        </wall>

Thank you.

like image 694
alvatar Avatar asked Feb 13 '10 19:02

alvatar


1 Answers

SSAX does not have any support to do type conversion, because as you correctly noticed XML lacks any semantic information. All you can do is putting some scheme constrains on your attributes. But SSAX has no support for XSD. So it would not be of any use.

But converting all strings to numbers is quite simple, because the parsed XML is stored in lists and dealing with lists is the most natural thing in Scheme. ;-)

(define example '((wall (@ (uid "1"))
                        (pt (@ (x "1.0") (y "2.0")))
                        (pt (@ (x "3.0") (y "4.0"))))))

(define (ssax:string->number ssax)
  (if (string? ssax)
      (string->number ssax)
      (if (list? ssax)
          (map ssax:string->number ssax)
          ssax)))

(ssax:string->number example)

But you will loose the leading zeros on your uids. They might be significant.

like image 94
ceving Avatar answered Oct 16 '22 21:10

ceving