Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 6 of 6

Thread: How can I pass an xsl:param to an ASP.NET function or variable

  1. #1
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Question How can I pass an xsl:param to an ASP.NET function or variable

    Hi all-

    I have a custom XML Package defined under each product in the ExtensionData field as follows:

    Code:
    <DownloadFileLocation>
          <dFile>descriptions/productdownloads/XX.html</dFile>
    </DownloadFileLocation>
    Each product then uses the product.tabbedUI.xml.config template which has been modified to add an additional tab called Downloads.

    The idea is that the dFile XML field will store the path to an external html file which contains the product's related downloads ie. software updates, documentation, etc. This file will be displayed inline in the Downloads tab.

    I wanted to use
    Code:
    <% Response.WriteFile() %>
    to include the external html file but I cannot figure out how to pass the XML param value to the ASP.NET function or how can I store it in an ASP.NET variable?

    For testing purposes, I have been able to retrieve the dFile param value and simply display the file path on the page using the following:

    under xsl:template:
    Code:
    <xsl:param name="myDFile" select="/root/Extensions/Product/ExtensionData/DownloadFileLocation/dFile"></xsl:param>
    where I want the data to appear:
    Code:
    <xsl:value-of select="$myDFile" disable-output-escaping="yes"/>
    In essence what I want to accomplish is:
    Code:
    <% Response.WriteFile(myDfile); %>

    Any suggestions?

    Thanks in advance for the help!

  2. #2
    webopius is offline Senior Member
    Join Date
    Nov 2008
    Location
    London, UK
    Posts
    440

    Default

    Hi, at last a chance to show a neat feature of XSLT... how to embed C# within an ASPDotNetStorefront XMLPackage:

    Step 1
    In your XML Package, edit the xsl:stylesheet section and add:
    Code:
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    Step 2
    Then, add "msxsl" to the exclude-result-prefixes setting so that it looks something like exclude-result-prefixes="aspdnsf msxsl"

    Step 3
    Insert this code just before your closing </xsl:stylesheet>:

    Code:
    <msxsl:script language="C#" implements-prefix="msxsl">
    <msxsl:using namespace="System.IO" />
    <![CDATA[
    public string showfile(string thefile)
    {
    	StreamReader streamReader = new StreamReader(thefile);
    	string contents = streamReader.ReadToEnd();
    	streamReader.Close();
    	
    	return contents;
    }
    ]]>
    </msxsl:script>
    Step 4

    Finally, insert this line at the point you want to show the file contents:

    Code:
    <xsl:value-of select="msxsl:showfile('c:\\temp\\test.txt')"/>
    Of course, replacing the test.txt example above with your own variable or value.


    It's not perfect and needs error handling added but it should get you started on the right track. This approach can be used to embed custom functions into XmlPackages without having to write independent XSLT packages.

    Adam
    Webopius.com

  3. #3
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Wink

    Hi Adam,

    Thanks a lot for the reply. I'll give it a shot!

  4. #4
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Question

    Hi Adam-

    I was able to get it to work by pulling my variable file path like so:

    Code:
    <xsl:value-of select="msxsl:showfile($myDFile)"/>
    This method seems to require an absolute directory path, my attempts to use relative paths have failed. So I wanted to hard code the directory path in the code and only retrieve the file name from the variable but I can't seem to get the right syntax down.

    I have tried all of the following but neither worked:

    Code:
    <xsl:value-of select="msxsl:showfile('c:\downloads\',$myDFile)"/>
    
    <xsl:value-of select="msxsl:showfile('c:\downloads\' + $myDFile)"/>
    
    <xsl:value-of select="msxsl:showfile('c:\downloads\' & $myDFile)"/>
    I also tried adding the sting in the function but that did not work either:

    Code:
    public string showfile(string thefile)
    {
         thefile = "c:\downloads\" + thefile;					
    					
         StreamReader streamReader = new StreamReader(thefile);
         string contents = streamReader.ReadToEnd();
         streamReader.Close();
    	
         return contents;
    }
    any suggestions?

    Thanks again for the help

  5. #5
    webopius is offline Senior Member
    Join Date
    Nov 2008
    Location
    London, UK
    Posts
    440

    Default

    Yes, it does currently use absolute paths. If you want to use them, and assuming your file really is in c:\downloads, then this should work using \\ instead of \:

    Code:
    public string showfile(string thefile)
    {
         thefile = "c:\\downloads\\" + thefile;					
    					
         StreamReader streamReader = new StreamReader(thefile);
         string contents = streamReader.ReadToEnd();
         streamReader.Close();
    	
         return contents;
    }
    If your file is within your store's web directory, you can do something like this, with this example the file is in the web site's images directory (using / this time just to be different!):

    Code:
    thefile = Server.MapPath("./images/filename.txt");
    Last edited by webopius; 09-28-2011 at 02:34 PM. Reason: Cant type

  6. #6
    omairkha is offline Member
    Join Date
    Aug 2011
    Posts
    89

    Default

    Hi Adam,

    I wasn't able to get it to work. For some reason it really does not like me attempting to add strings. So I am just passing the entire path in the XMLPackage and that is the only way it seems to be working fine.

    Thanks again for the help