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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With