Need ASP.NET Help
I’m trying to convert some regular ASP code to ASP.NET to deal with UTF-8/Unicode issues that keep cropping up when I perform transforms in traditional ASP. Alas, I can’t seem to figure out the correct syntax, objects, classes to make it happen. Its amazing how you can learn to do something in one programming language and the basic logic is the same in another but if you don’t know the exact code, translating can take forever. I have had similar issues when morphing ASP code into PHP code. Although nothing has been quite this maddening in a while.
Anyone out there know anything about ASP.NET and how to do transforms (XSL) of XML with it? A basic transform ie. here is my XML here is my XSL is easy. However, I need to pass parameters to my XSL. I just spend most of the day trying to figure it out and a hour in the bookstore looking at books. I can’t seem to figure out how to pass a parameter to my XSL file and then do the transform. At least I can’t figure out how to do it in ASP.NET using VB not C#. I’ve found things that look like they might be the right sort of code but no specific examples where a parameter is being passed.
If you can help or have suggestions please email me the assistance will be greatly appreciated.
It’s not that hard, really ;-)
____________________________________________________________
Sample1.vb
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Namespace Samples
Module XsltParams
Sub Main()
Try
Dim xml As XmlDocument = New XmlDocument
Dim xsl As XslTransform = New XslTransform
Dim arg As XsltArgumentList = New XsltArgumentList
Dim res As XmlUrlResolver = New XmlUrlResolver
Dim txt As TextWriter = Console.Out
xml.Load(“../XsltParams.xml”)
xsl.Load(“../XsltParams.xsl”)
arg.AddParam(“title”, “”, “Sample for the Library Web Chic”)
xsl.Transform(xml, arg, txt, res)
Catch ex As Exception
Console.Error.WriteLine(“Exception: {0}”, ex.Message)
End Try
Console.ReadLine()
Exit Sub
End Sub
End Module
End Namespace
____________________________________________________________
Sample1.xml
____________________________________________________________
Sample1.xsl
label { font-weight: bold; }
Looks like you need to quote those greater than, less than, and ampersands in the submital text box… Lets try it again:
_______________________________________________________________
Sample1.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<document>
<property name=”author” value=”Andrew Houghton” />
<property name=”mediator” value=”Andrew Houghton” />
<property name=”audience” value=”Library Web Chic” />
<property name=”title” value=”VB.NET XSLT parameter sample” />
</document>
_______________________________________________________________
Sample1.xsl
<?xml version=”1.0″ encoding=”utf-8″ ?>
<xsl:transform version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
>
<xsl:output method=”xml” version=”1.0″
media-type=”text/xml” encoding=”UTF-8″
omit-xml-declaration=”yes” indent=”yes”
/>
<xsl:param name=”title” select=”string(”)” />
<xsl:template name=”Root” match=”/document[1]“>
<html>
<head>
<title><xsl:value-of select=”$title” /></title>
<meta http-equiv=’Content-Type’ content=’text/html; charset=utf-8′ />
<style type=’text/css’>
label { font-weight: bold; }
</style>
</head>
<body>
<table cellpadding=’1′ cellspacing=’1′>
<caption><xsl:value-of select=”$title” /></caption>
<xsl:for-each select=”property”>
<tr>
<td><label><xsl:value-of select=”@name” /></label></td>
<td><span><xsl:value-of select=”@value” /></span></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:transform>
Oops, one more time. The names of the files should be:
XsltParams.vb instead of Sample1.vb
XsltParams.xml instead of Sample1.xml
XsltParams.xsl instead of Sample1.xsl
Don’t forget to change the path location, depending upon where you put them, e.g.:
xml.Load(â€../XsltParams.xmlâ€)
xsl.Load(â€../XsltParams.xslâ€)
Easiest way to build in VB.NET, create an empty project, copy the files into the project directory, right click on the files in solution explorer and include them in the project, then build the solution.
Okay, I got this far and things work. Here is my real question.
In the following line,
arg.AddParam(â€titleâ€, “â€, “Sample for the Library Web Chicâ€)
you have a string as the parameter.
How do I put a variable in this spot so that I can pass the parameter from a web form? Just sticking the variable name there doesn’t work
Karen
Just change arg.AddParam line to:
Dim param As String
If Environment.GetCommandLineArgs.GetLowerBound(0) > 1 Then
param = Environment.GetCommandLineArgs.GetValue(1).ToString()
Else
param = “Sample for the Library Web Chic”
End If
arg.AddParam(“title”, “”, param)
or have I missed the point and you are asking for something else?
BTW, since you mentioned that you are using ASP.NET Web pages, are you aware of the XML server control? It’s handy when you have a form page and you want to display the results of some XML transformed in a variety of ways. Here is a sample for you. First create a new project based on a Web Form. Right click on the *.aspx in Solution Explorer and select delete. Copy the following files into the project directory under your IIS root, then right click on each one and select include in project. Right click on the XsltParamsForm.aspx file a select it as the startup page. Rebuild the soultion and have fun ;-)
PS. If you wanted to have multiple transformations, you could add a dropdown that returned you an XSLT filename, then in the Page.isPostBack = True section you could change the value of xmlResult.TransformSource to the value in the dropdown. Ditto for xmlResult.DocumentSource.
_____________________________________________________________
XsltParamsForm.aspx
<%@ Page Language=”vb” AutoEventWireup=”false” Codebehind=”XsltParamsForm.aspx.vb” Inherits=”XsltParams.Samples.XsltParamsForm” responseEncoding=”utf-8″ culture=”en-US” uiCulture=”en-US”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<title>XsltParamsForm</title>
<META http-equiv=Content-Type content=”text/html; charset=utf-8″>
<meta name=”GENERATOR” content=”Microsoft Visual Studio .NET 7.1″>
<meta name=”CODE_LANGUAGE” content=”Visual Basic .NET 7.1″>
<meta name=vs_defaultClientScript content=”JavaScript”>
<meta name=vs_targetSchema content=”http://schemas.microsoft.com/intellisense/ie5″>
</HEAD>
<body MS_POSITIONING=”GridLayout”>
<form id=”Form1″ method=”post” runat=”server”>
<asp:Label id=lblValue style=”Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 24px” runat=”server” Width=”304px” Font-Bold=”True” Font-Names=”Tahoma”>Enter XSLT parameter value:</asp:Label>
<asp:TextBox id=tbxValue style=”Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 48px” runat=”server” Width=”304px” Font-Names=”Tahoma” Wrap=”False”>Sample for the Library Web Chic</asp:TextBox>
<INPUT style=”Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 80px” type=submit value=Submit>
<div style=”Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 120px”>
<asp:Xml id=xmlResult runat=”server” DocumentSource=”XsltParams.xml” TransformSource=”XsltParams.xsl”></asp:Xml>
</div>
</form>
</body>
</HTML>
_____________________________________________________________
XsltParamsForm.aspx.vb
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Namespace Samples
Public Class XsltParamsForm
Inherits System.Web.UI.Page
#Region ” Web Form Designer Generated Code ”
‘This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents lblValue As System.Web.UI.WebControls.Label
Protected WithEvents tbxValue As System.Web.UI.WebControls.TextBox
Protected WithEvents xmlResult As System.Web.UI.WebControls.Xml
‘NOTE: The following placeholder declaration is required by the Web Form Designer.
‘Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
‘CODEGEN: This method call is required by the Web Form Designer
‘Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack = True Then
xmlResult.TransformArgumentList = New XsltArgumentList
xmlResult.TransformArgumentList.AddParam(“title”, “”, tbxValue.Text)
xmlResult.Visible = True
Else
xmlResult.Visible = False
End If
Exit Sub
End Sub
End Class
End Namespace
_____________________________________________________________
XsltParams.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<document>
<property name=”author” value=”Andrew Houghton” />
<property name=”mediator” value=”Andrew Houghton” />
<property name=”audience” value=”Library Web Chic” />
<property name=”title” value=”VB.NET XSLT parameter sample” />
</document>
_____________________________________________________________
XsltParams.xsl
<?xml version=”1.0″ encoding=”utf-8″ ?>
<xsl:transform version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
>
<xsl:output method=”xml” version=”1.0″
media-type=”text/xml” encoding=”UTF-8″
omit-xml-declaration=”yes” indent=”yes”
/>
<xsl:param name=”title” select=”string(”)” />
<xsl:template name=”Root” match=”/document[1]“>
<html>
<head>
<title><xsl:value-of select=”$title” /></title>
<meta http-equiv=’Content-Type’ content=’text/html; charset=utf-8′ />
<style type=’text/css’>
label { font-weight: bold; }
</style>
</head>
<body>
<table cellpadding=’1′ cellspacing=’1′>
<caption><xsl:value-of select=”$title” /></caption>
<xsl:for-each select=”property”>
<tr>
<td><label><xsl:value-of select=”@name” /></label></td>
<td><span><xsl:value-of select=”@value” /></span></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:transform>