This topic contains 3 replies, has 3 voices, and was last updated by Eliot Muir 8 years, 9 months ago.
XML Oddity
-
Consider this snippet:
function main() local X = xml.parse{data='<SPACETIME></SPACETIME>'} local tim = X.SPACETIME:append(xml.ELEMENT,'TIME') tim:setInner(os.date()) local spc = X.SPACETIME:append(xml.ELEMENT,'SPACE') spc:setInner(' ') trace(X:S()) end
The trace() shows the XML as:
<SPACETIME> <TIME>Fri Aug 29 17:00:48 2014</TIME> <SPACE></SPACE> </SPACETIME>
Note the lack of space in SPACE.
However, when I do this:
function main() local X = xml.parse{data='<SPACETIME></SPACETIME>'} local tim = X.SPACETIME:append(xml.ELEMENT,'TIME') tim:setInner(os.date()) local spc = X.SPACETIME:append(xml.ELEMENT,'SPACE') spc:setInner('X') spc[1] = ' ' trace(X:S()) end
The trace shows this:
<SPACETIME> <TIME>Fri Aug 29 17:02:35 2014</TIME> <SPACE> </SPACE> </SPACETIME>
Note that there is now a space character in SPACE.
I can’t find any other way (outside of building the XML by hand) to insert a space character into an XML element. & # x 2 0 ; (sorry for the weird representation, the forum editor seems to clobber that) doesn’t work either; it just gets stripped out. Thoughts?
Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com
I had a peak at the code and it looks like setInner is doing an XML parse of the data – the white space is probably being ignored. That is my first guess.
We have an interface that requires fields be valued with a space character when the data isn’t present in the source system, and at this stage convincing the subscribers to that data to change their design isn’t feasible.
The setInner() / node[1] = ‘ ‘ combo is the only mechanism that seems to work, but it feels like a hack and I’m worried the behavior might change in future versions of Iguana. Is there a better way to accomplish this?
Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com
The code above wouldn’t be broken. I’d suggest making a helper node function to do this in any case since it will make the code easier to read and mean there is only one place to alter the behavior.
Created a knowledge base page Setting an XML node to a space ” ” character with the code for several ways to do this.
In this case both the first and second options are a good fit. The first option uses the
node.setText()
function from the xml module. The second option has the advantage that it uses the builtinnode.append()
function (so it does not require the xml custom module).First option:
require 'xml' -- uses setText() from the xml module function main() local X = xml.parse{data='
'} local tim = X.SPACETIME:append(xml.ELEMENT,'TIME') tim:setInner(os.date()) local spc = X.SPACETIME:append(xml.ELEMENT,'SPACE') spc:setText(' ') -- use node.setText() instead of node.setInner() trace(X:S()) end Second option:
function main() local X = xml.parse{data='
'} local tim = X.SPACETIME:append(xml.ELEMENT,'TIME') tim:setInner(os.date()) local spc = X.SPACETIME:append(xml.ELEMENT,'SPACE') spc:append(xml.TEXT, ' ') -- use node.append() instead of node.setInner() trace(X:S()) end
You must be logged in to reply to this topic.