saxon9EE - javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet - vb.net

I am trying to transform our system data (in xml format) to defined output xml file.
To create this output xml, I am planning to use XSLT version 2.0. Hence I'm using Saxon EE evaluation version (SaxonEE9-5-1-2 version)
I am trying to demonstrate this software to my team thereby buy the software and use the features and functionality of XSLT 2.0, XQuery, etc
Currently I am working on a prototype project - where I am having problem.
VB.NET Transformation code is as follows:
Dim strXSLT As String = String.Empty
Dim strXML As String = String.Empty
'Retrieve data from database as string
strXML = GetData()
Dim processor As Saxon.Api.Processor = New Saxon.Api.Processor()
Dim builder As Saxon.Api.DocumentBuilder = processor.NewDocumentBuilder()
'Retrieve XSLT file content from database as string
strXSLT = GetXSLT()
'Convert XSLT string data as memorystream
Dim byteDataXSLT() As Byte
byteDataXSLT = System.Text.Encoding.UTF8.GetBytes(strXSLT)
Dim msXSLT As New System.IO.MemoryStream(byteDataXSLT,0, byteDataXSLT.Length)
'Convert XML string data as memorystream
Dim byteDataXML() As Byte
byteDataXML = System.Text.Encoding.UTF8.GetBytes(strXML)
Dim msXML As New System.IO.MemoryStream(byteDataXML,0, byteDataXML.Length)
'Save the xml file before processing
Dim fsXML As New System.IO.FileStream("C:\Temp\XML.xml", System.IO.FileMode.OpenOrCreate)
Dim byteFileXML() As Byte
byteFileXML = msXML.ToArray()
fsXML.Write(byteFileXML, 0, byteFileXML.Length)
'Save the xsl file before processing
Dim fsXSLT As New System.IO.FileStream("C:\Temp\XSL.xslt", System.IO.FileMode.OpenOrCreate)
Dim byteFileXSLT() As Byte
byteFileXSLT = msXSLT.ToArray()
fsXSLT.Write(byteFileXSLT, 0, byteFileXSLT.Length)
Dim sURI As New Uri("file:///C:/")
builder.BaseUri = sURI
Dim input As Saxon.Api.XdmNode = builder.Build(msXML)
Dim transformer As Saxon.Api.XsltTransformer = processor.NewXsltCompiler().Compile(msXSLT).Load()
transformer.InitialContextNode = input
Dim serializer As New Saxon.Api.Serializer()
serializer.SetOutputFile(strOutputFileName)
transformer.Run(serializer)
I am getting below error on below line, only when I run through program. When I do the transformation manually i am not getting this error.
Dim transformer As Saxon.Api.XsltTransformer = processor.NewXsltCompiler().Compile(msXSLT).Load()
Error Message:
saxon9ee javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet.
But when I saved the xml and xslt memorystream to filestream and do the transformation manually, we get the desired output.
We are trying to figure out the root cause of this issue, but no luck.
Any details on this error message would be really helpful and very much appreciated.
Please let me know if you need more information.
Many Thanks in advance
Raghu

Looks like you are not seeing the error messages produced by the compiler. If you aren't running from the Console, then set the ErrorList property on the XsltCompiler, and when the exception occurs, inspect and display the errors somewhere, e.g. send them to a log file or pop up a window containing them.
Without seeing the error message, I can't begin to guess what the error might be.

Related

VB.NET Save XMLdocument as UTF-8 with BOM

I have written a test application to modify a few hundred XMLs, basically I modify a few nodes and then save the XML again.
The input XML files are UTF-8-BOM but the output is UTF-8 (as viewed in Notepad++).
The code runs on a console application in VB.NET 4.7.2 and this is my basic code:
Dim myXML As XmlDocument = New XmlDocument
Dim nodelist As XmlNodeList
Dim node As XmlNode
myXML.Load(file)
nodelist = myXML.SelectNodes("//root/row")
For Each node In nodelist
'All my code goes here
Next
myXML.Save(file)
I tried things like:
myXML.CreateXmlDeclaration("1.0", "UTF-16", "")
But that didn't work. I have been searching and it seems everyone has the exact oposite issue and checking in the MSDOC I can't see any reference to specify BOM or not:
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldeclaration.encoding?view=netframework-4.7
The issue is that when the files are imported to the DB without BOM some characters show corruption so I really need the same encoding.
After so much reading I could try to rewrite my app using StreamWriter instead XMLDocument but if there is a workaround about that I would pretty much prefer it :). Thanks!
As suggested by #JosefZ I used this strategy:
Using writer = New XmlTextWriter(file, New UTF8Encoding(True))
Dim xmlsettings As XmlWriterSettings = New XmlWriterSettings
xmlsettings.OmitXmlDeclaration = False
xmlsettings.ConformanceLevel = ConformanceLevel.Fragment
xmlsettings = writer.Settings
myXML.Save(writer)
End Using

Saving embedded resource contents to string

I am trying to copy the contents of an embedded file to a string in Visual Basic using Visual Studio 2013. I already have the resource (Settings.xml) imported and set as an embedded resource. Here is what I have:
Function GetFileContents(ByVal FileName As String) As String
Dim this As [Assembly]
Dim fileStream As IO.Stream
Dim streamReader As IO.StreamReader
Dim strContents As String
this = System.Reflection.Assembly.GetExecutingAssembly
fileStream = this.GetManifestResourceStream(FileName)
streamReader = New IO.StreamReader(fileStream)
strContents = streamReader.ReadToEnd
streamReader.Close()
Return strContents
End Function
When I try to save the contents to a string by using:
Dim contents As String = GetFileContents("Settings.xml")
I get the following error:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
Which occurs at line:
streamReader = New IO.StreamReader(fileStream)
Nothing else I've read has been very helpful, hoping someone here can tell me why I'm getting this. I'm not very good with embedded resources in vb.net.
First check fileStream that its not empty as it seems its contains nothing that's why you are getting a Null exception.
Instead of writing to file test it by using a msgBox to see it its not null.
fileStream is Nothing because no resources were specified during compilation, or because the resource is not visible to GetFileContents.
After fighting the thing for hours, I discovered I wasn't importing the resource correctly. I had to go to Project -> Properties -> Resources and add the resource from existing file there, rather than importing the file from the Solution Explorer. After adding the file correctly, I was able to write the contents to a string by simply using:
Dim myString As String = (My.Resources.Settings)
Ugh, it's always such a simple solution, not sure why I didn't try that first. Hopefully this helps someone else because I saw nothing about this anywhere else I looked.

Illegal Characters in Path Error When Downloading CSV File

I need download a CSV file and then read it. Here is my code:
tickerValue = "goog"
Dim strURL As String = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
Dim strBuffer As String = RequestWebData(strURL)
Using streamReader = New StreamReader(strBuffer)
Using reader = New CsvReader(streamReader)
I keep getting this error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Illegal characters in path.
What am I doing wrong?
Additional Info
In another part of my program I use this code and it works fine.
Address = http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download
Dim strBuffer As String = Historical_Stock_Prices.RequestWebData(Address)
Using streamReader = New StringReader(strBuffer)
Using reader = New CsvReader(streamReader)
Isn't my second code the same concept as my problem code?
you are giving it, essentially, a web url. somewhere in your code, it does not support the web url. it could be the streamreader. it could be the CsvReader.
what line of code does this point to?
the best bet is to save the file TO DISK, then read from disk.
UPDATE
here is an example to SAVE to disk:
using writer as new StreamWriter("C:\Test.csv")
writer.Write(strBuffer)
writer.Close()
end using
here is an example to READ from disk:
using strReader as new StreamReader("C:\Test.csv")
' this code is presumably how it works for reading into the CsvReader:
using reader as new CsvReader(strReader)
' now do your thing
end using
strReader.Close()
end using

Set a report datasource instance at run time

I have created a report that is based on a business object - this works great. I am now trying to add a button that renders the report directly to PDF (in a winforms application).
I know what I need to do - in code I am creating a ReportViewer, setting the DataSource, specifying the report (it's an embedded resource), then rendering the report into a byte array before using System.IO.File.WriteAllBytes to flush the byte array to disk. One thing I'm hung up on though, is how do I specify the instance of the object properly? I keep getting the "An error has occurred during the report processing" error. In IntelliTrace I can see that an exception is thrown "A data source instance has not been supplied for the data source 'IssRep'" (IssRep is the dataset name in the report. Here is the code:
Dim warning As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimetype As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim viewer As New ReportViewer
Dim bs As New BindingSource
bs.DataSource = issuedet
Dim rds As New ReportDataSource
rds.Value = bs
viewer.LocalReport.DataSources.Add(rds)
viewer.ProcessingMode = ProcessingMode.Local
viewer.LocalReport.ReportEmbeddedResource = "FRSFE.SR.rdlc"
Dim pdfbytes As Byte()
Try
pdfbytes = viewer.LocalReport.Render("PDF", Nothing, mimetype, encoding, extension, streamids, warning)
File.WriteAllBytes("C:\Shared\FRS\SR.PDF", pdfbytes)
Catch ex As Exception
MsgBox(ex.Message)
End Try
I'm pretty sure whatever I'm stuck on is pretty simple as I'm very rusty on .NET but I just can't figure it out!
Try setting rds.Name = "IssRep" before adding it to viewer.LocalReport.DataSources.

Deserialize Object from String with StringReader

I am currently able to Serialize an object and store the data in a database by using the following code:
Dim x As New XmlSerializer(myObject.GetType)
Dim sw As New IO.StringWriter()
x.Serialize(sw, MyObject)
'Write sw to database
I can't figure out how to Deserialize the data from the database into the object.
I've tried using the reverse with no success. (The problem with the code below is that the strReader is null:)
Dim x As New Xml.Serialization.XmlSerializer(GetType(myObject))
Dim strReader As New StringReader(dr("xml_data") & "")
Dim tmpData As myObject = CType(x.Deserialize(strReader), myObject)
Any assistance will be greatly appreciated.
Refer to follow MS KB article that talks about serialization and deserialization.(KB Article).
You code should be fine as per the code. But, sharing the code of your object help in identification other possible issues.