I'm working in Excel 2010 using VBA. I have a column in a MS SQL server 2008 database table that contains a single line of XML in it. I'm extracting it and using a 'prettyprint' routine I wrote to make it readable. The issue that I'm running into is that it doesn't take the value I give it in the xslt transformation. As you can see, I've tried quite a few combinations that I've found doing different google searches. Here is the code:
Function PrettyPrintXML(strXML As String, Optional sFileOut As String) As String
Dim xDoc As MSXML2.DOMDocument60
Set xDoc = New MSXML2.DOMDocument60
Dim xDocStyle As MSXML2.DOMDocument60
Set xDocStyle = New MSXML2.DOMDocument60
Dim xDocOut As MSXML2.DOMDocument60
Set xDocOut = New MSXML2.DOMDocument60
Dim strXMLStyleSheet As String
strXMLStyleSheet = "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & " extension -element - prefixes = ""redirect""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & " xmlns:xalan=""http://xml.apache.org/xalan"">" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & " xmlns:xalan=""http://xml.apache.org/xslt"">" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "<xsl:output method=""xml"" encoding=""UTF-8"" indent=""yes"" xalan:indent-amount=""2""/>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "<xsl:output method=""xml"" indent=""yes"" xalan:indent-amount=""2""/>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "<xsl:strip-space elements=""*""/>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "<xsl:template match=""@* | node()"">" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & " <xsl:copy>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & " <xsl:apply-templates select=""@* | node()""/>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & " </xsl:copy>" & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "</xsl:template>" & vbCrLf & vbCrLf
strXMLStyleSheet = strXMLStyleSheet & "</xsl:stylesheet>"
'strXMLStyleSheet = "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "Version = ""1.0""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "xmlns: redirect = ""http://xml.apache.org/xalan/redirect""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "extension-element-prefixes = ""redirect""" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "xmlns:xalan=""http://xml.apache.org/xslt"">" & vbCrLf
'strXMLStyleSheet = strXMLStyleSheet & "<xsl:output method=""xml"" indent=""yes"" xalan:indent-amount=""2""/>" & vbCrLf
With xDocStyle
.async = False
.validateOnParse = False
.resolveExternals = False
DebugPrint strXMLStyleSheet
.LoadXML strXMLStyleSheet
End With
With xDoc
.async = False
.validateOnParse = False
.resolveExternals = False
DebugPrint strXML
.LoadXML strXML
.transformNodeToObject xDocStyle, xDocOut
End With
PrettyPrintXML = xDocOut.XML
If Not sFileOut = "" Then
If Not PrettyPrintXML = "" Then
Dim ffile As String
ffile = FreeFile
Open sFileOut For Output As #ffile
Print #ffile, PrettyPrintXML
Close #ffile
Else
MsgBox "No file was generated. Check your parameters and try again.", vbOKOnly, "Empty File"
End If
End If
Set xDocOut = Nothing
Set xDocStyle = Nothing
Set xDoc = Nothing
End Function
The xslt transform in the latest version of code is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml" indent="yes" xalan:indent-amount="2"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
The interesting part is if I open it in Notepad++, it does indent 2 like I want but it changes the encoding to utf-16. If I open it in Notepad, it also changes the encoding to utf-16 but doesn't change the spacing. I've tried changing it to utf-16 as well as ASCII. No change though. My ultimate goal is to give the user a choice of opening it in Notepad, wordpad, MS Word 2010 or any custom program they want like Ultraedit, textedit, vi, Emacs, etc. I have to get this working first though.