Set MSXML2.XSLTemplate60.stylesheet gives variable or parameter error before I have a chance to supply them

141 Views Asked by At

I have an XSLT that I want to test for processing XML data through MSXML2 in VBA.

The usual use of this XSLT is to process XML data from MicroStation or OpenRoads Designer which supplies these parameters according to settings in the report browser.

In my case, as I'm trying to run some of the report XML through MSXML2, I find that this error comes up:

A reference to variable or parameter 'xslStationPrecision' cannot be resolved. The variable or parameter may not be defined, or it may not be in scope.

This is true even as I'm repurposing code from Microsoft's own documentation here: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762312(v=vs.85)

To be specific, here's my code block that duplicates the sample code:

Sub test_xsl_from_ms_learn()
    ' from https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762312(v=vs.85)
    
    Dim xslt As New MSXML2.XSLTemplate60
    Dim xsldoc As New MSXML2.FreeThreadedDOMDocument60
    Dim xslproc
    
    Dim PathToXslFile As String
    
    PathToXslFile = "my_own_custom.xsl"
    
    xsldoc.async = False
    xsldoc.Load (PathToXslFile)
    
    If xsldoc.parseError.ErrorCode <> 0 Then
        Debug.Print xsldoc.parseError.reason
    Else
        On Error Resume Next
            Set xslt.stylesheet = xsldoc
            Debug.Print Err.Number, Err.Description
            'this is where I get the error
            'if not for this error, I would advance through to where I would use addParameter
        On Error GoTo 0
    End If
End Sub

How can I use addParameter before this error?

1

There are 1 best solutions below

0
Derek Schmidt On

I found the solution. First, through testing I found that a reference within the XSLT file covers the gap with the parameters, so I needed to resolve external references.

But after resolving that, I got a new error "Security settings do not allow the execution of script code within this stylesheet." Through a bit of searching, I found the answer from Microsoft here: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms763800(v=vs.85)?redirectedfrom=MSDN

So the following lines are needed before Set xslt.stylesheet:

        xsldoc.resolveExternals = True 
            'this wasn't mentioned in the Learn code, but it is essential to resolve this error:
            'A reference to variable or parameter 'xslStationPrecision' cannot be resolved.  The variable or parameter may not be defined, or it may not be in scope.
        xsldoc.SetProperty "AllowXsltScript", True 
            'to avoid this error for having <msxsl:script>:
            'Security settings do not allow the execution of script code within this stylesheet.

In the end, this code block loads properly:

Sub test_xsl_from_ms_learn()
    ' from https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762312(v=vs.85)
    
    Dim xslt As New MSXML2.XSLTemplate60
    Dim xsldoc As New MSXML2.FreeThreadedDOMDocument60
    Dim xslproc
    
    Dim PathToXslFile As String
    
    PathToXslFile = "my_own_custom.xsl"
    
    xsldoc.async = False
    xsldoc.Load (PathToXslFile)
    
    If xsldoc.parseError.ErrorCode <> 0 Then
        Debug.Print xsldoc.parseError.reason
    Else
        xsldoc.resolveExternals = True 
        xsldoc.SetProperty "AllowXsltScript", True 
        On Error Resume Next
            Set xslt.stylesheet = xsldoc
            Debug.Print Err.Number, Err.Description
        On Error GoTo 0
    End If
End Sub