0

I have a xslt file that contains a xsl variable that I am using like a lookup table.

<xsl:variable name="TestLookup">
    <lookup code="A" means="Test A"/>
    <lookup code="B" means="Test B"/>
    <lookup code="C" means="Test C"/>
</xsl:variable>

Then I call it like this.

<xsl:value-of select="document('')/*/xsl:variable[@name='TestLookup']/lookup[@code=current()]/@means"/>

When I call the Transform method in .NET I get the following errors:

An error occurred while loading document ''
This operation is not supported for a relative URI.

Basically it is telling me that it can't find the document.
I need some way of creating a lookup table in the xslt file and be able to call it with the Transform command in .NET.

This article had the same problem but I didn't see an answer. Something about p/2 function?
It also listed the node-set() but I can't find any good article on using node-set as a lookup table.
using document() function in .NET XSLT generates error

Adding More Code

.NET CODE

' get the xml from SQL    
Dim xmlData As String = sqlXmlReader.Item("XmlData").ToString()

' read the xslt file
Using styleSheet = New StreamReader("XSLTTestFile.xslt")
    ' load the stylesheet from a resource
    Using styleSheetReader As XmlReader = XmlReader.Create(styleSheet)
        Dim xslt = New XslCompiledTransform()
        Dim xsltSettings = New XsltSettings(True, False)            
        xsltSettings.EnableDocumentFunction = True

        ' load the stylesheet for transformation
        xslt.Load(styleSheetReader, xsltSettings, New XmlUrlResolver())
        Using stringWriter As New System.IO.StringWriter
            ' transform the xml document along with the stylesheet
            Dim xmlDoc = New XmlDocument()
            xmlDoc.LoadXml(xmlData)
            xslt.Transform(xmlDoc, Nothing, stringWriter)
            ' dump the transformation to the browser control
            _htmlString.Append(stringWriter.ToString())
        End Using
    End Using
End Using
4
  • The answer was to use XsltSettings. Did you try that? Commented Dec 8, 2014 at 19:49
  • possible duplicate of using document() function in .NET XSLT generates error Commented Dec 8, 2014 at 19:49
  • Yes I did try the New XsltSettings(True, False) that is how I get the error and this might be a duplicate (as noted in the bottom of my explanation) but they never really answered the original. Commented Dec 8, 2014 at 20:05
  • Please show the code you used. This will reduce the confusion about which code you used. Commented Dec 8, 2014 at 20:07

2 Answers 2

1

There is an overload of the Load method that takes a string with the file name or URL of the stylesheet, use that and XslCompiledTransform knows how to resolve document(''). So use xslt.Load("XSLTTestFile.xslt", xsltSettings, new XmlUrlResolver()).

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

3 Comments

The only thing that I can find to use is the XmlUrlReslover but that requires ResolveUri which I don't have a uri to the xsl. The xsl is on the file system not on the web.
Pass that file name to the Load method instead of wrestling with an XmlReader over a StreamReader.
That fixed the problem. Used Load("fileName",New XsltSettings(True, True), New XmlUrlResolver())... Thanks.
0

In my case I had a document('../xml/fieldcodes.xml') in my xslt. The following code was working for net452:

Dim readerSettings = New XmlReaderSettings()
readerSettings.DtdProcessing = DtdProcessing.Parse
readerSettings.XmlResolver = New XmlUrlResolver() ' required for net452 and later
Using sr = New StringReader(XMLstr), sourceReader As XmlReader = XmlReader.Create(sr, readerSettings)
    Dim sb = New System.Text.StringBuilder()
    Using writer = XmlWriter.Create(sb)
        xmlvalidTransformer.Transform(sourceReader, New XsltArgumentList(), writer) 
        Using resReader = New StringReader(sb.ToString()), reader As XmlReader = XmlReader.Create(resReader, readerSettings)
            myResultDoc.Load(reader)
        End Using
    End Using
End Using

When switching to .net core, this code throws the following exception:

Exception: System.Exception: An error occurred while loading document '../xml/fieldcodes.xml'. See InnerException for a complete description of the error.
 ---> System.Xml.Xsl.XslTransformException: An error occurred while loading document '../xml/fieldcodes.xml'. See InnerException for a complete description of the error. ---> System.Xml.XmlException: Resolving of external URIs was prohibited.
   at System.Xml.XmlNullResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
:

I could fix it by changing the XslCompiledTransform.Transform() method to this overload:

xmlvalidTransformer.Transform(sourceReader, New XsltArgumentList(), writer, New XmlUrlResolver()) ' New XmlUrlResolver() is required for .net core

The following line did fixed it as well, but I did not like to turn on this in general:

AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true)

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.