Subject: Newbie: ASP.NET/XML Transformation Problem
Posted By: kwilliams Post Date: 8/28/2006 10:02:25 AM
I'm trying to convert an ASP page that uses XML/XSLT Transformation into an ASP.NET page that uses the same transformation. The old ASP page was like this:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" aspcompat="true" Debug="true" %>
<%@ OutputCache Duration="10" VaryByParam="*"%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Text.RegularExpressions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
'Declare querystring variables
Dim page As String = Request.QueryString("page")
Dim dir As String = Request.QueryString("dir")
Dim subdir1 As String = Request.QueryString("subdir1")
Dim subdir2 As String = Request.QueryString("subdir2")
Dim subleftnav As String = Request.QueryString("subleftnav")
%>
<%
'Path separater
Dim path_slash As String = "/"

'Declare transformed variables
Dim page_path As String, dir_path As String, subdir1_path As String, subdir2_path As String

'If dir is empty
If dir = "" Then
dir_path = ""
Else
dir_path = dir + path_slash
End If

'If subdir1 is empty
If subdir1 = "" Then
subdir1_path = ""
Else
subdir1_path = subdir1 + path_slash
End If

'If subdir2 is empty
If subdir2 = "" Then
subdir2_path = ""
Else
subdir2_path = subdir2 + path_slash
End If

'If page is not empty
If page <> "" Then
page_path = page
End If
%>
<%
'Declare XML and XSL file paths
Dim xmlURL As String, xslURL As String
xmlURL = "http://SERVERNAME/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xml/" + page_path + ".xml"
xslURL = "http://SERVERNAME/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xslt/" + page_path + ".xsl"
'Test
'Response.Write(xmlURL + "<br />" + xslURL)
%>
<%
'Load XML
'Dim xml = Server.CreateObject("Microsoft.XMLDOM") 'DELETE LATER
Dim xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
xml.async = false
xml.load(xmlURL)

'Load XSL
'Dim xsl = Server.CreateObject("Microsoft.XMLDOM") 'DELETE LATER
Dim xsl = Server.CreateObject("MSXML2.DOMDocument.3.0")
xsl.async = false
xsl.load(xslURL)

'Transform file
Response.Write(xml.transformNode(xsl))
%>


...and the new ASP.NET page I've developed is so far like this:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" AspCompat="true" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Text.RegularExpressions" %>
<script runat="server">

    Sub Page_Load

        'To import XML/XSLT Transformation using DOM
        'Declare querystring variables
        Dim page As String = Request.QueryString("page")
        Dim dir As String = Request.QueryString("dir")
        Dim subdir1 As String = Request.QueryString("subdir1")
        Dim subdir2 As String = Request.QueryString("subdir2")
        Dim subleftnav As String = Request.QueryString("subleftnav")

        'Path separater
        Dim path_slash As String = "/"

        'Declare transformed variables
        Dim page_path As String, dir_path As String, subdir1_path As String, subdir2_path As String

        'If dir is empty
        If dir = "" Then
        dir_path = ""
        Else
        dir_path = dir + path_slash
        End If

        'If subdir1 is empty
        If subdir1 = "" Then
        subdir1_path = ""
        Else
        subdir1_path = subdir1 + path_slash
        End If

        'If subdir2 is empty
        If subdir2 = "" Then
        subdir2_path = ""
        Else
        subdir2_path = subdir2 + path_slash
        End If

        'If page is not empty
        If page <> "" Then
        page_path = page
        End If

        'Declare XML and XSL file paths
        Dim xmlURL As String, xslURL As String
        xmlURL = "http://SERVERNAME/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xml/" + page_path + ".xml"
        xslURL = "http://SERVERNAME/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xslt/" + page_path + ".xsl"
        'Response.Write(xmlURL + "<br />" + xslURL) 'Test

        'Load XML
        'Dim xml = Server.CreateObject("Microsoft.XMLDOM") 'DELETE LATER
        Dim xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
        xml.async = false
        xml.load(xmlURL)

        'Load XSL
        'Dim xsl = Server.CreateObject("Microsoft.XMLDOM") 'DELETE LATER
        Dim xsl = Server.CreateObject("MSXML2.DOMDocument.3.0")
        xsl.async = false
        xsl.load(xslURL)

        'Transform file
        'Response.Write(xml.transformNode(xsl)) 'This method DOES work

    End Sub

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
    <form runat="server">
        <asp:Xml id="xslTransform" runat="server" DocumentSource="<%=(xmlURL)%>" TransformSource="<%=(xslURL)%>"></asp:Xml>
    </form><!-- This method DOES NOT work -->
</body>
</html>


When I use this property to transform the 2 files:
'Transform file
        'Response.Write(xml.transformNode(xsl))

...it comes through fine.

But when I attempt to use this method to transform the 2 files:
<form runat="server">
        <asp:Xml id="xslTransform" runat="server" DocumentSource="<%=(xmlURL)%>" TransformSource="<%=(xslURL)%>"></asp:Xml>
    </form>

...I receive this error message:
 Exception Details: System.ArgumentException: Illegal characters in path.
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I'm wanting to use the second method because I want to load the data directly in the <body> tag of the page. If anyone can see what I'm doing wrong, and let me know how to correct it, that would be greatly appreciated. Thanks.

KWilliams
Reply By: kwilliams Reply Date: 8/28/2006 2:40:02 PM
Well, I solved this one after further research and testing. I just needed to assign the form field xmlTransorm within the code of the document to retain a dynamic page. So this is what I did:

<script language="VB" runat="server">
Dim xmlURL As String, xslURL As String
xmlURL = "/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xml/" + page_path + ".xml"
xslURL = "/MAINDIRECTORY/" + dir_path + subdir1_path + subdir2_path + "docs/xslt/" + page_path + ".xsl"
'Assign dynamic urlS for this page
xslTransform.DocumentSource = xmlURL
xslTransform.TransformSource = xslURL
</script>
<html>
<body>
<form runat="server">
 <asp:Xml id="xslTransform" runat="server"></asp:Xml>
</form>
</body>
</html>

...and it worked great. There's a great online article that comes from the book "XSLT for ASP.NET" that talks about this at http://www.topxml.com/dotnet/articles/xslt/default.asp. Even thought I code in VB.NET, and this book uses C# for its examples, I definitely intend to get this book.

KWilliams

Go to topic 48963

Return to index page 191
Return to index page 190
Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182