kr094
kr094

Reputation: 139

Format XML string in VBScript

I have a string of unformatted xml (no whitespacing) and I want to make a VBScript function that accepts the string as its parameter and formats the XML with tabs and newlines

I have taken a good look around the net and came close with this http://blogs.msdn.com/b/robert_mcmurray/archive/2012/07/06/creating-quot-pretty-quot-xml-using-xsl-and-vbscript.aspx

This did not work because the 'MSXML2.DomDocument' object does not support writing to a string from what I can tell.

Ive tried to access various properties of the object (namely 'xml', 'text', and 'xml.text') all to no avail.

Simply put I need a string of messy xml in, and a string of formatted xml out

Upvotes: 4

Views: 4844

Answers (2)

GELR
GELR

Reputation: 1293

All credits to Ekkehard. I included modifications if you want to use the script in an Asp Classic application.

Function prettyXml(ByVal sDirty)
    '****************************************
    '* Put whitespace between tags. (Required for XSL transformation.)
    '****************************************
    sDirty = Replace(sDirty, "><", ">" & vbLf & "<")

    '****************************************
    '* Create an XSL stylesheet for transformation.
    '****************************************
    Dim objXSL : Set objXSL = Server.CreateObject("Msxml2.DOMDocument")
    objXSL.loadXML  "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _
                    "<xsl:output method=""xml"" indent=""yes""/>" & _
                    "<xsl:template match=""/"">" & _
                    "<xsl:copy-of select="".""/>" & _
                    "</xsl:template>" & _
                    "</xsl:stylesheet>"

    '****************************************
    '* Transform the XML.
    '****************************************
    Dim objXML : Set objXML = Server.CreateObject("Msxml2.DOMDocument")
    objXML.loadXml sDirty
    objXML.transformNode objXSL
    prettyXml = objXML.xml
End Function

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

All credit goes to Robert McMurray; I just reworked his script into a function:

Option Explicit

' ****************************************
Function prettyXml(ByVal sDirty)
' ****************************************
' Put whitespace between tags. (Required for XSL transformation.)
' ****************************************
  sDirty = Replace(sDirty, "><", ">" & vbCrLf & "<")
' ****************************************
' Create an XSL stylesheet for transformation.
' ****************************************
  Dim objXSL : Set objXSL = WScript.CreateObject("Msxml2.DOMDocument")
  objXSL.loadXML  "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _
                  "<xsl:output method=""xml"" indent=""yes""/>" & _
                  "<xsl:template match=""/"">" & _
                  "<xsl:copy-of select="".""/>" & _
                  "</xsl:template>" & _
                  "</xsl:stylesheet>"
' ****************************************
' Transform the XML.
' ****************************************
  Dim objXML : Set objXML = WScript.CreateObject("Msxml2.DOMDocument")
  objXML.loadXml sDirty
  objXML.transformNode objXSL
  prettyXml = objXML.xml
End Function

Dim sTest : sTest = "<a><b><c/></b></a>"
WScript.Echo           sTest
WScript.Echo "----------"
WScript.Echo prettyXml(sTest)
WScript.Quit 0

output:

cscript robmcm-2.vbs
<a><b><c/></b></a>
----------
<a>
        <b>
                <c/>
        </b>
</a>

On 2nd thought:

You shouldn't use the above unless you have studied this.

Upvotes: 4

Related Questions