Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA XML transformation ignores included Stylesheets?

I am trying to transform an XML file into a Microsoft Word document, using VBA. However when I try to run the macro, I get an error stating:
Error ‘-2147467259 (80004005)’: The template ‘example’ doesn’t exist in the stylesheet.

My transformation looks like this:

Public Function Transform(strXML As String, strXSLT As String, strOutput As String, _
         Optional strError As String) As Long

     Dim objXML As MSXML2.DOMDocument60
     Dim objXSLT As MSXML2.DOMDocument60
     Dim objOutput As MSXML2.DOMDocument60

     Set objXML = New MSXML2.DOMDocument60
     objXML.Load strXML
     If objXML.parseError = 0 Then
         Set objXSLT = New MSXML2.DOMDocument60
         objXSLT.Load strXSLT
         If objXSLT.parseError = 0 Then
             Set objOutput = New MSXML2.DOMDocument60
             objXML.transformNodeToObject objXSLT, objOutput
             objOutput.Save strOutput
         Else
             Transform = objXSLT.parseError.errorCode
             strError = ".xslt-file: " & vbCrLf & strXSLT & vbCrLf & objXSLT.parseError.reason
         End If
     Else
         Transform = objXML.parseError.errorCode
         strError = ".xml-file: " & vbCrLf & strXML & vbCrLf & objXML.parseError.reason
     End If

End Function

My XML-file is just fine.
The XSL-file however contains an include at the top like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Word.Document"?>
 
<xslt:stylesheet version="1.0"
    xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
    xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
    xmlns:r="http://schemas.openxmlformats.org/package/2006/relationships">
    
    <xslt:output method="xml" encoding="UTF-8" indent="yes"/>
    <xslt:strip-space elements="*"/>
    
    <xslt:include href="layout.xsl"/>
    <xslt:include href="calculations.xsl"/>
    <xslt:include href="functions.xsl"/>
    <xslt:include href="styles.xsl"/>

It seems to me the VBA doesn’t load those included files. Is there a way to make this possible?

>Solution :

You need, for MSXML 6, to explicitly set a property objXSLT.resolveExternals = True before loading the XSLT code.

See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762283(v=vs.85)#script-syntax saying "In MSXML 6.0, the default setting is False" and "If this property is set to False, no external includes and imports will be resolved".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading