{"id":18,"date":"2009-09-21T23:23:35","date_gmt":"2009-09-22T03:23:35","guid":{"rendered":"http:\/\/halnesbitt.com\/blog\/?p=18"},"modified":"2016-09-01T00:05:45","modified_gmt":"2016-09-01T04:05:45","slug":"pass-querystring-variable-to-xslt","status":"publish","type":"post","link":"https:\/\/halnesbitt.com\/blog\/2009\/09\/21\/pass-querystring-variable-to-xslt\/","title":{"rendered":"Pass QueryString Variable to XSLT"},"content":{"rendered":"<p><!--more-->So I was building an annual meeting program that needed to grab certain nodes from a remote XML file based on a session ID. I figured I could just pass the session id as a querystring variable and the XSLT file could manipulate it, but it turned out to be slightly more complicated than that. Being the novice that I am, I thought I ought to document it. It really consists of three parts: 1) grabbing the querystring, 2) pass the variable to the XSLT as an argument, and 3) using the passed paramenter in the XSLT to output the correct HTML. <\/p>\n<p>Here is the page load for the aspx page, in VB. Note that I also added code to cache the remote XML file on the server, as this greatly increases the loading speed:<\/p>\n<pre>\r\n<code>\r\n  Sub Page_Load(sender As Object, e As EventArgs)\r\n\r\n    'Grabbing the querystring = the sess_id to look up the session\r\n    Dim sess_id As String\r\n\tsess_id = Request.QueryString(\"sess_id\")\r\n\r\n\r\n    'Grabbing the remote XML file\r\n    Dim strXmlSrc  As String = \"http:\/\/www.yourremoteXMLfile.com\"\r\n\r\n    ' Path to our XSL file. \r\n    Dim strXslFile As String = Server.MapPath(\"stylethisthing.xsl\")\r\n\r\n    'This adds the querystring variable to be passed to the XSL\r\n    Dim args As XsltArgumentList = New XsltArgumentList\r\n    args.AddParam(\"sess_id\", \"\", sess_id)\r\n\r\n  'Check to see if the cached XML file exists. If not, cache it and expire it after 1 hour (absolute, regardless of user)\r\n  If Cache.Item(\"RWProgram\") Is Nothing Then\r\n    ' Load our XML file into the XmlDocument object.\r\n    Dim myXmlDoc As XmlDocument = New XmlDocument()\r\n    myXmlDoc.Load(strXmlSrc)\r\n    Cache.Insert(\"RWProgram\", myXmlDoc, Nothing, DateTime.Now.AddMinutes(60), TimeSpan.Zero)\r\n  End If\r\n\r\n    ' Load our XSL file into the XslTransform object.\r\n    Dim myXslDoc As XslTransform = New XslTransform()\r\n    myXslDoc.Load(strXslFile)\r\n\r\n    'Use a stringbuilder to write the HTML\r\n    Dim myStringBuilder As StringBuilder = New StringBuilder()\r\n    Dim myStringWriter  As StringWriter  = New StringWriter(myStringBuilder)\r\n\r\n    ' Transform this junks\r\n    myXslDoc.Transform(Cache.Item(\"RWProgram\"), args, myStringWriter)\r\n\r\n    ' Spit out the HTML\r\n    programdetails.Text = myStringBuilder.ToString\r\n\r\n\r\n  End Sub\r\n<\/code>\r\n<\/pre>\n<p>Then, add this code to the top of your XSLT:<\/p>\n<pre>\r\n<code>\r\n&lt;xsl:param name=\"sess_id\" \/&gt;\r\n<\/code>\r\n<\/pre>\n<p>You can now reference the &#8220;sess_id&#8221; parameter anywhere in your XSLT with &#8220;$sess_id&#8221;. I used this in my XPATH to grab the correct node for the meeting session, so that we had a fully dynamic meeting program. Not the most advanced thing, but it works \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-computer-stuff","category-show-all"],"_links":{"self":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":11,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/halnesbitt.com\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}