Apply XSLT with arguments against XML
Here's a simple function for your toolbox for applying XSL to XML and passing an array of arguments that can be used as parameters in the stylesheet. If you haven't needed this yet... You will. ;~)
Code:
<%
Function transformXMLArguments(strXMLDoc, strXSLDoc, arrParams)
Set xml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
xml.async = false
xml.load strXMLDoc
Set xsl = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
xsl.async = false
xsl.load strXSLDoc
Set template = Server.CreateObject("MSXML2.XSLTemplate")
template.stylesheet = xsl
set processor = template.createProcessor()
processor.input = xml
For i = 0 to UBound(arrParams)
If arrParams(i) <> "" Then
processor.addParameter "param" & (i+1), arrParams(i)
End If
Next
processor.transform()
transformXMLArguments = (processor.output)
Set xml = nothing
Set xsl = nothing
Set template = nothing
set processor = nothing
End Function
%>
It would be used as follows if you are using parameters in your stylesheet:
<%
myarray = array("some text", 23, 34)
response.write transformXMLArguments(Server.MapPath("../xml/sitemap.xml"), Server.MapPath(xslTemp), myarray)
%>
You can access your parameters in your stylesheet by instantiating them with the prefix param and their number in the array. I started with 1. so in your stylesheet for this example you'll have something like:
<xsl:param name="param1" /> <!--this would be: "some text" -->
<xsl:param name="param2" /> <!--this is: 23 -->
<xsl:param name="param3" /> <!--this is: 34 -->
Or you can use it without parameters like so:
<%
response.write transformXMLArguments(Server.MapPath("xml-file.xml"), Server.MapPath("xsl-file.xsl"), "")
%>
Have fun - Andrew
Read a File into a variable
Here's a function that I made long ago to read the contents of a file in a variable. This function accepts the file name as parameter & is a bit raw, but it serves my purpose, so hopefully it'll serve others as well. :D
Code:
Private Function readMyFile(pFileName)
SET FSO = Server.CreateObject("Scripting.FileSystemObject")
IF (TRIM(pFileName)="") THEN
EXIT FUNCTION
ELSE
IF FSO.FileExists(Server.MapPath(pFileName)) THEN
SET mFile = FSO.OpenTextFile(Server.MapPath(pFileName))
readMyFile = mFile.ReadAll
ELSE
readMyFile = "File Not Found"
END IF
END IF
SET mFile = NOTHING
SET FSO = NOTHING
End Function
Its usage is quite simple.
Code:
strFileName = "data/myFile.asp"
strFileContent = readMyFile(strFileName) 'Data of file read up into the variable