4

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I realize there is no need to format (The xml my script produces is valid). This is just for output to a file/db
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.