Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 7th, 2006, 03:58 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Parameter to ASP.NET Variable

I'm trying to pass an XSLT parameter into an ASP.NET page, and back again, but it's not getting passed for some reason. If someone could let me know what I'm doing wrong, and possible help me out with a solution, it would be greatly appreciated. Thanks.

XML DOC:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<feedback>
    <feedback id="1">
        <fname>FIRSTNAME</fname>
        <lname>LASTNAME</lname>
    </feedback>
</feedback>
XSLT DOC:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:param name="fname_val" select="''" />
<xsl:param name="lname_val" select="''" />

<xsl:variable name="fname_msg">Must enter between 1 and 30 characters</xsl:variable>
<xsl:variable name="lname_msg">Must enter between 1 and 30 characters</xsl:variable>
    <xsl:template match="/">
        <form method="post" action="feedback.aspx">
            <table class="navyblueborder" width="100%">
                <xsl:for-each select="feedback/feedback">
                    <tr>
                        <td class="whiteheader" bgcolor="#333366" colspan="2">Feedback Form</td>
                    </tr>
                    <tr>
                        <td bgcolor="#ccccff"><strong>First Name:</strong></td>
                        <td bgcolor="#ccccff"><input type="text" id="txtfname" name="txtfname" value="fname_test" size="30" maxlength="30" tabindex="1" />
                        *
                        <xsl:if test="$fname_val != ''">
                            <xsl:value-of select="$fname_msg" />
                        </xsl:if>

                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#ccccff"><strong>Last Name:</strong></td>
                        <td bgcolor="#ccccff"><input type="text" id="txtlname" name="txtlname" value="lname_test" size="30" maxlength="30" tabindex="2" />
                        *</td>
                    </tr>
                </xsl:for-each>
            </table>
        </form>
    </xsl:template>
</xsl:stylesheet>
ASP.NET DOC (feedback.aspx):
Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<%
    'Declare XML and XSL file paths
    Dim xmlURL As String, xslURL As String
    xmlURL = "feedback.xml"
    xslURL = "feedback.xsl"

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

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

    'Pull in form data
    Dim fname_form As String = trim(Request.Form("txtfname")) 'REQUIRED
    Dim lname_form As String = trim(Request.Form("txtlname")) 'REQUIRED

    'Test pulled in form data
    Response.Write("Form FName: " + fname_form + "<br />") 'TEST
    Response.Write("Form LName: " + lname_form + "<br />") 'TEST

    'Pull in XSL parameters
    Dim fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
    fname_param.SetAttribute("select", "'" & fname_form & "'")
    Response.Write("XML FName: " + fname_param + "<br />") 'TEST

    Dim lname_param As String = xsl.selectSingleNode("//xsl:param[@name='lname_val']")
    fname_param.SetAttribute("select", "'" & lname_form & "'")
    Response.Write("XML LName: " + lname_qs + "<br />") 'TEST
%>
RESULTING ERROR:
Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error:

Line 49: Dim fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
Line 50: fname_param.SetAttribute("select", "'" & fname_form & "'") '<!---***ERROR IS ON THIS LINE***
Line 51: Response.Write("XML FName: " + fname_param + "<br />") 'TEST
Line 52:

KWilliams
 
Old February 8th, 2006, 04:33 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Personally I wouldn't use MSXML COM in ASP.NET as there are known issues but to solve your problem is straightforward.
You need to alert the processor to the fact that you're going to use the "xsl" prefix as a short form of the XSLT namespace: http://www.w3.org/1999/XSL/Transform

So add these lines after creating your xsl object and before loading the XSL itself:
Code:
xsl.setProperty"SelectionLanguage", "XPath"
xsl.setProperty "SelectionNamespaces", "xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"""
This method of setting the variables is a bit of a hack, you should really replace the xsl:variable elements with xsl:param and use code similar to:

http://msdn.microsoft.com/library/de...asp?frame=true


--

Joe (Microsoft MVP - XML)
 
Old February 9th, 2006, 12:59 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello joe,

I followed your directions, but I'm still receiving the same error message of "Object variable or With block variable not set" when I submit the form. I know that the resulting asp.net page isn't seeing the parameter for some reason, but I can't figure out why. Any other suggestions? Thanks for your help.

P.S. What would be your suggestion of a better way to set up a server-side transformation of XML & XSLT into XHTML?

KWilliams
 
Old February 9th, 2006, 01:19 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Are you sure the xml and xslt are loading?
Code:
Response.Write xslUrl & ": " & xsl.load(xslURL)
--

Joe (Microsoft MVP - XML)
 
Old February 9th, 2006, 01:27 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, they are loading properly. Is it not possible to have one ASP.NET doc for the transformation/form display, and another backend ASP.NET script for the validation and resetting of XSLT parameters?

The only success I've had so far with setting XSLT parameters with ASP.NET variables is when the XML doc, XSLT stylesheet, and VB script are loaded in the same ASP.NET transformation page. I've set these pages up in pretty much the same way, so that's why I'm puzzled.

KWilliams
 
Old February 9th, 2006, 01:40 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
Dim fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
Code:
=>
Dim fname_param as IXmlDomNode
fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
fname_param.setAttribute("select", "'" & fname_form & "'")
and similarly for the other param.

As I said before .NET XML is better, I'll post some code later.


--

Joe (Microsoft MVP - XML)
 
Old February 9th, 2006, 01:56 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just made your suggested changes, like this:

Dim fname_param As IXmlDomNode
    fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
    fname_param.SetAttribute("select", "'" & fname_form & "'")

...but now I'm receiving this error message:
Type 'IXmlDomNode' is not defined.

KWilliams
 
Old February 9th, 2006, 02:05 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I forgot you're using createObject, I don't do much VB.NET so maybe try:
Code:
Dim fname_param As Object
    fname_param = xsl.selectSingleNode("//xsl:param[@name='fname_val']")
    fname_param.SetAttribute("select", "'" & fname_form & "'")

--

Joe (Microsoft MVP - XML)
 
Old February 9th, 2006, 02:57 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's now getting the old error "Object variable or With block variable not set.". I figure that it's got to be something small that I'm doing wrong on my side. I'll keep working on it, and I'll let you know when/if I come up with a solution.

Concerning other options to my setup, I'd love to see some sample code from you when you have the time. Thanks joe...I really appreciate your help.

KWilliams
 
Old February 9th, 2006, 04:27 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joe,

I figured out a solution (hopefully). I figured that the problem likely lied in the form submitting to another ASP.NET doc, since I'm using a completely dynamic setup for my new site. So I created a SSI document that will contain properties for each form that I have on my site, and included it on the dymanic index page. Once I did that, the XSLT to ASP.NET parameter setup worked great.

Thanks again for your help, and I look forward to hearing from you in the future concerning alternatives to this setup...especially .NET XML.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Pass Parameter to Sub Report in asp.net 2.0 jawad Ahmed Crystal Reports 2 March 1st, 2008 02:17 AM
assign XSLT variable to a wml variable arunagottimukkala XSLT 2 October 19th, 2007 05:15 AM
ASP.NET XSL Parameter Problem kwilliams ASP.NET 2.0 Basics 1 July 6th, 2007 04:43 PM
Pass parameter from XSLT to ASP.NET kwilliams XSLT 3 August 17th, 2005 11:56 AM
passing parameter from asp.net to crystal report diyagp Crystal Reports 0 October 26th, 2004 05:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.