Creating "Pretty" XML using XSL and VBScript
I was working with an application recently that stored all of its settings in a large XML file, however, when I opened the XML in Windows Notepad, all I saw was a large blob of tags and text - there was no structured formatting to the XML, and that made it very difficult to change some of settings by hand. (Okay - I realize that some of you are probably thinking to yourselves, maybe I wasn't supposed to be editing those settings by hand - but that's just the way I do things around here... if I can't customize every setting to my heart's content, then it's just not worth using.)
In any event, I'll give you an example of what I mean by using the example XML database that's provided on MSDN at the following URL:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms762271.aspx
Note - the entire XML file would be too long to repost here, so I'll just include an unstructured except from that file that resembles what my other XML looked like when I opened the file in Windows Notepad:
<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description></book><book id="bk103"><author>Corets, Eva</author><title>Maeve Ascendant</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-11-17</publish_date><description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description></book></catalog>
This is obviously difficult to read, and even more so when you are dealing with hundreds or thousands of lines of XML code. What would be considerably easier to read and edit would be something more like the following example:
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-11-17</publish_date> <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description> </book> </catalog>
I had written a "Pretty XML" script sometime around ten years ago that read an XML file, collapsed all of the whitespace between tags, and then inserted CRLF sequences and TAB characters in order to reformat the file. This script worked great for many years, but I decided that it would be more advantageous to use XSL to transform the XML. (e.g. "Why continue to do things the hard way when you really don't need to?");-]
With that in mind, I rewrote my old script as the following example:
' ****************************************
' MAKE PRETTY XML
' ****************************************
Option Explicit
Const strInputFile = "InputFile.xml"
Const strOutputFile = "OutputFile.xml"
' ****************************************
Dim objInputFile, objOutputFile, strXML
Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim objXML : Set objXML = WScript.CreateObject("Msxml2.DOMDocument")
Dim objXSL : Set objXSL = WScript.CreateObject("Msxml2.DOMDocument")
' ****************************************
' Put whitespace between tags. (Required for XSL transformation.)
' ****************************************
Set objInputFile = objFSO.OpenTextFile(strInputFile,1,False,-2)
Set objOutputFile = objFSO.CreateTextFile(strOutputFile,True,False)
strXML = objInputFile.ReadAll
strXML = Replace(strXML,"><",">" & vbCrLf & "<")
objOutputFile.Write strXML
objInputFile.Close
objOutputFile.Close
' ****************************************
' Create an XSL stylesheet for transformation.
' ****************************************
Dim strStylesheet : strStylesheet = _
"<xsl:stylesheet version=""1.0"" xmlns:xsl=""https://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.
' ****************************************
objXSL.loadXML strStylesheet
objXML.load strOutputFile
objXML.transformNode objXSL
objXML.save strOutputFile
WScript.Quit
This script is really straightforward in what it does:
- Creates two MSXML DOM Document objects:
- One for XML
- One for XSL
- Creates two file objects:
- One for the input/source XML file
- One for the output/destination XML
- Reads all of the source XML from the input file.
- Inserts whitespace between all of the XML tags in the source XML; this is required or the XSL transformation will not work properly.
- Saves the resulting XML into the output XML file.
- Dynamically creates a simple XSL file that will be used for transformation in one of the MSXML DOM Document objects.
- Loads the output XML file from earlier into the other MSXML DOM Document object.
- Transforms the source XML into well-formatted ("pretty") XML.
- Replaces the XML in the output file with the transformed XML.
That's all that there is to it.
Note: For more information about the XSL stylesheet that I used, see https://www.w3.org/TR/xslt.
Comments
Anonymous
July 06, 2012
Try xmlNotepadAnonymous
July 06, 2012
@Jimbo - LOL, I actually use xmlNotepad for some projects; it depends on what I'm doing. But I use this script all the time.Anonymous
November 10, 2014
Hi @Jimbo, great post mate## I'm new in XML and this is exactly what I need; Can you help me by telling how can I use the script?{(can you give some example, please ?) like, I can take the script and save in XML format or need to be use in html format in base to run } Thank you. StefanAnonymous
April 17, 2015
Hi, Not sure but I think solution is to use parser and/or SAX method.Anonymous
October 01, 2016
The comment has been removed