Converting XML to HTML
-
I have an XML file which has the XSL file specified in the first line: 1380852 …. And the rest of the XML data follows …. I need to transform and output this as HTML. How can this be achieved in ASP.NET / VB.NET ? I have tried using the xmlTransform class, but it doesn’t seem to like the XSL reference embedded in the XML file. E.g: Dim myXslTransform As XslTransform Dim myXPathDocument As XPathDocument myXPathDocument = New XPathDocument("C:\test\test99.xml") myXslTransform = New XslTransform() ' myXslTransform.Load("") 'I don't have a separate .XSL stylesheet... Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter() myXslTransform.Transform(myXPathDocument, Nothing, stWrite) Response.Write(stWrite.ToString) Gives the following error: 'xml:stylesheet' is an invalid name for a processing instruction. Line 1, position 3. Changing the first line of the XML file to read (dash instead of colon) gets rid of this error, but then I no longer have the XSL link, and it just outputs ‘raw’ XML! My end goal is to be able to read in multiple XML files in this format and output them to ONE browser page as HTML. Any help is much appreciated. John.
-
I have an XML file which has the XSL file specified in the first line: 1380852 …. And the rest of the XML data follows …. I need to transform and output this as HTML. How can this be achieved in ASP.NET / VB.NET ? I have tried using the xmlTransform class, but it doesn’t seem to like the XSL reference embedded in the XML file. E.g: Dim myXslTransform As XslTransform Dim myXPathDocument As XPathDocument myXPathDocument = New XPathDocument("C:\test\test99.xml") myXslTransform = New XslTransform() ' myXslTransform.Load("") 'I don't have a separate .XSL stylesheet... Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter() myXslTransform.Transform(myXPathDocument, Nothing, stWrite) Response.Write(stWrite.ToString) Gives the following error: 'xml:stylesheet' is an invalid name for a processing instruction. Line 1, position 3. Changing the first line of the XML file to read (dash instead of colon) gets rid of this error, but then I no longer have the XSL link, and it just outputs ‘raw’ XML! My end goal is to be able to read in multiple XML files in this format and output them to ONE browser page as HTML. Any help is much appreciated. John.
The correct declaration is:
<?xml-stylesheet type="text/xsl" href="_URI of your stylesheet_"?>
If you open the file in Internet Explorer, you will see the result of the transformation. TheXslTransform
class will not detect the stylesheet from the processing instruction. You will have to extract the URI and load the stylesheet yourself. For example:Imports System.Xml.XPath
Imports System.Text.RegularExpressions
...
Public Shared Function GetStylesheet(ByVal doc As IXPathNavigable) As String
If doc Is Nothing Then Return String.EmptyDim nav As XPathNavigator = doc.CreateNavigator() If Not nav.MoveToFirstChild() Then Return String.Empty Dim href As New Regex(" href=\['""\](?<href>\[^'""\]\*)\['""\]", \_ RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture) Do If XPathNodeType.ProcessingInstruction = nav.NodeType \_ AndAlso "xml-stylesheet" = nav.Name Then Dim m As Match = href.Match(nav.Value) If Not m Is Nothing Then Return m.Groups("href").Captures(0).Value End If End If While nav.MoveToNext() Return String.Empty
End Function
...
Dim myXPathDocument As New XPathDocument("C:\test\test99.xml")
Dim myXslTransform As New XslTransform()
myXslTransform.Load(GetStylesheet(myXPathDocument))
Dim stWrite As New System.IO.StringWriter()
myXslTransform.Transform(myXPathDocument, Nothing, stWrite)
Response.Write(stWrite.ToString)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The correct declaration is:
<?xml-stylesheet type="text/xsl" href="_URI of your stylesheet_"?>
If you open the file in Internet Explorer, you will see the result of the transformation. TheXslTransform
class will not detect the stylesheet from the processing instruction. You will have to extract the URI and load the stylesheet yourself. For example:Imports System.Xml.XPath
Imports System.Text.RegularExpressions
...
Public Shared Function GetStylesheet(ByVal doc As IXPathNavigable) As String
If doc Is Nothing Then Return String.EmptyDim nav As XPathNavigator = doc.CreateNavigator() If Not nav.MoveToFirstChild() Then Return String.Empty Dim href As New Regex(" href=\['""\](?<href>\[^'""\]\*)\['""\]", \_ RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture) Do If XPathNodeType.ProcessingInstruction = nav.NodeType \_ AndAlso "xml-stylesheet" = nav.Name Then Dim m As Match = href.Match(nav.Value) If Not m Is Nothing Then Return m.Groups("href").Captures(0).Value End If End If While nav.MoveToNext() Return String.Empty
End Function
...
Dim myXPathDocument As New XPathDocument("C:\test\test99.xml")
Dim myXslTransform As New XslTransform()
myXslTransform.Load(GetStylesheet(myXPathDocument))
Dim stWrite As New System.IO.StringWriter()
myXslTransform.Transform(myXPathDocument, Nothing, stWrite)
Response.Write(stWrite.ToString)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for the reply and the code Richard - I like the 'GetStyleSheet()' function! - I have a feeling you've been through this before? :-) The XML files I'm using were created by a different Dev team, I have no idea why they are using