Testing conformity of xml data

Sarah Reichelt sarah.reichelt at gmail.com
Tue Jan 29 01:38:23 EST 2008


 > check "tidy" unix program. Tidy is a popular unix tool that every host
> out there will have installed. Even your MacOS X bundles it. It is
> used by a lot of toolkits. You can use a combination of tidy and
> shell() call to test your XML.

Andre's mention of tidy made me wonder if it could be used to produce
formatted XML. Rev's XML library can give a text representation of an
XML tree using revXMLtext, but it is not formatted and appears all on
one line. There is an undocumented parameter to revXMLtext that does a
limited amount of formatting, but it still is not great. I tried
writing my own and it works fine but only for small XML files.

Here is what I came up with to use tidy and it can be altered to check
for errors, or to return neatly indented XML text:

    -- get the text of the current XML tree as returned by Rev's XML library
    put revXMLText(tDocID) into tXMLtext

    -- save to temporary file
    -- (should check for a unique name here, but adding the seconds to
the filename seems enough)
    put specialfolderpath("Desktop") & "/temp" & the seconds & ".xml"
into tFileName
    put tXMLtext into URL ("file:" & tFileName)

    -- to check and indent the text (returns formatted text or a list of errors)
    put "tidy -iq -xml " & tFileName into tCmd

    -- or just to look for errors (returns empty or a list of errors)
    -- put "tidy -eq -xml " & tFileName into tCmd

    put shell(tCmd) into tTidyText
    replace space & space with tab in tTidyText    -- tidy indents by
2 spaces, but I prefer tabs
    put tTidyText into fld "XMLtext"

    -- get rid of the temporary file
    delete file tFileName

The tidy utility is included in Mac OS X and is available for Windows
<http://tidy.sourceforge.net/>. I don't know about Linux - can anyone
tell me if it is usually included?

Cheers,
Sarah



More information about the use-livecode mailing list