0
votes

I am tying to get xslt2 transformation using XQSharp, but i get an exception when trying to call ApplyTemplates.

My code:

<WebMethod()>
Public Function test(ByVal inputXml As String, ByVal inputXsl As String) As String
        Dim nameTable As XmlNameTable = New NameTable()

        Dim xmlReaderSettings As New XmlReaderSettings()
        xmlReaderSettings.NameTable = nameTable

        Dim document As XdmDocument

        Using reader As New StringReader(inputXml)
            document = New XdmDocument(reader)
        End Using

        Dim querySettings As New XsltSettings(nameTable)
        querySettings.ContextItemType = XdmType.Node
        querySettings.ModuleResolver = New XmlUrlResolver()

        Dim query As Xslt = Xslt.Compile(New StringReader(inputXsl), 
querySettings)

        Dim contextItem As XPathNavigator = document.CreateNavigator()
        Dim result As Stream = New MemoryStream()
        query.ApplyTemplates(contextItem, result)


        Using reader As StreamReader = New StreamReader(result)
            Return reader.ReadToEnd()
        End Using

End Function

XmlInput:

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>hoi</title>
</head>
<body>
    <p>Test</p>
</body>
</html>

XslInput:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" exclude-result-prefixes="xhtml xsl fn xs xdt">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
</xsl:stylesheet>

Exception

System.ArgumentNullException was unhandled by user code
Message=Value cannot be null.
Parameter name: format
ParamName=format
Source=mscorlib
StackTrace:
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at XQSharp.XdmException.WrongParamNameTable(LineInfo lineInfo, XmlQualifiedName parameterName)
at XQSharp.DocumentSet.ImportParamNode(LineInfo lineInfo, XPathNavigator navigator, XmlQualifiedName parameterName)
at XQSharp.DynamicContext.ConvertArgument(IEnumerable`1 value, LineInfo lineInfo, DocumentSet documentSet, StaticModuleContext context, XmlQualifiedName name, BoundType declaredType)
at XQSharp.DynamicContext..ctor(StaticModuleContext staticContext, DynamicContextSettings settings, XmlQualifiedName initialMode, XmlQualifiedName initialTemplate, Int32 stackSpace, Int32 globalSpace, IResultDocumentHandler resultDocumentHandler)
at XQSharp.Xslt.Evaluate(XmlQualifiedName initialMode, XmlQualifiedName initialTemplate, DynamicContextSettings settings, IResultDocumentHandler resultDocumentHandler)
at XQSharp.Xslt.ApplyTemplates(IXPathNavigable contextNode, Stream resultDocument)
at Cmsservices.XSLTEngine.test(String inputXml, String inputXsl) in
D:\Projecten\cmsservices\App_Code\CmsservicesXSLTEngine.vb:line 44 InnerException:

What i am doing wrong?

1

1 Answers

1
votes

The ArgumentNullException was raised because we had incorrectly referenced the error message in a resource file. This has been fixed for the next version (2.2).

The problem is that the document is compiled with a different name table to the query.

The problem lies in the following lines of your source code:

Using reader As New StringReader(inputXml)
    document = New XdmDocument(reader)
End Using

You are not specifying the name table to use to construct the XdmDocument, and so a new name table is created for the document.

I was going to suggest that you should simply pass the name table to the constructors for XdmDocument, but have just noticed that we neglected to add constructors taking a name table. Again this should be improved in the next version.

To fix your query, construct your XdmDocument from an XmlReader instead:

Using reader As New StringReader(inputXml)
    Using xmlReader As XmlReader = XmlReader.Create(reader, xmlReaderSettings)
        document = New XdmDocument(xmlReader)
    End Using
End Using