What does attribute "as" do in "param" element? - xslt

I've faced with the next code in xls transformation:
<xsl:param name="e" as="element()"/>
I'm struggling, what does as="element()" mean here? I've checked several resources:
w3schools.com
developer.mozilla
But there is nothing about as attribute for param element. Do anybody know the meaning and purpose of the attribute?

The best documentation source is w3. From documentation:
<!-- Category: declaration -->
<xsl:param
name = eqname
select? = expression
as? = sequence-type
required? = boolean
tunnel? = boolean
static? = boolean >
<!-- Content: sequence-constructor -->
</xsl:param>
The xsl:param element has an optional as attribute, which specifies the required type of the parameter. The value of the as attribute is a SequenceType. If the as attribute is omitted, then the required type is item()*.
You can read more about "as" attribute in that section.
Thanks, michael.hor257k.

Related

how to give relative path into href if xsl:import

I am using xslt 1.0.
I want to import A.xsl into B.xsl.
I am unable to use relative paths in href attribute of xsl:import. for ex. path is c:/test/testdata/xsl/file/A.xsl
I tired following code
<xsl:variable name="filePath" select="concat(Systemprop:getProperty('docRootPath'),'/xsl/file/A.xsl')" />
and
<xsl:import href="{concat(Systemprop:getProperty('docRootPath'),'/xsl/file/A.xsl')}">
where docrootPath = c:/test/testdata
but its is giving error : Element type "xsl:variable" must be followed by either attribute specifications, ">" or "/>".
but its giving me :Had IO Exception with stylesheet file. Please suggest .
See the spec, http://www.w3.org/TR/xslt#section-Combining-Stylesheets, it defines the syntax for include and import as
<!-- Category: top-level-element -->
<xsl:include
href = uri-reference />
<xsl:import
href = uri-reference />
So the href attribute value needs to be a URI reference, neither an expression nor an attribute value template is allowed there. So putting in an XPath expression like concat(...) is not possible, is not supported.
A relative URI reference can be used of course, but you would need to make sure a base URI is defined properly, for instance doing <xsl:import xml:base="file:///C:/test/testdata/" href="file.xml"/>. That is however also only a static value to be defined during stylesheet authoring.

Using a parameter passed into xslt stylesheet

I am using Saxon to perform a transformation of an XML document in my .NET application. I am passing in a parameter to my xslt document but I have no idea how to use it in my template.
Here is what I have done so far:
var zipcode = _db.AXCustomers.FirstOrDefault(x => x.ACCOUNTNUM == accNo).ZIPCODE;
transformer.SetParameter(new QName("CustomerZipCode"), new XdmAtomicValue(zipcode));
Then in my xslt document I am specifying the parameter like so:
<xsl:template match="/">
<xsl:param name="CustomerZipCode" />
But when I try to use the parameter, nothing appears. I am using it like so:
<xsl:value-of select="substring-before($CustomerZipCode, ' ')"/>
But nothing is output even though my zipcode does contain a value
You are using xsl:param inside a xsl:template element, it means that the param is for the template. The parameter you are passing from the .net code is a transformer parameter and related xsl:param must be placed at the top level of the stylesheet, into the xsl:stylesheet element.

XSLT parameter is always empty <xsl:param name="mediaFolderId" select="/macro/mediaFolderId" />

I created an xslt called GraphicsRowSlider with the following parameters
<xsl:param name="mediaFolderId" select="/macro/mediaFolderId" />
<xsl:param name="title" select="/macro/title" />
And added the parameters title and mediaFolderId to the related macro.
I then created new Macro Container data type and selected the macro GraphicsRowSlider as allowed macro. I then added a new field of the new data type into a document type and then imported that field into a template.
Finally, from the content, I inserted the macro and added a title and selected media folder... However, I could see that the macro calls the correct xslt with the correct title and mediaFolderId but the parameters are always empty!
Any thought?! Note, I always get this
<?UMBRACO_MACRO macroalias="GraphicsRowSlider" title="Add Title here" mediaFolderId="1159" />
The syntax you are using suggests that you are using an old umbraco version, which one are you using?
The definition how to work with macro parameters can be found on the umbraco wiki: http://our.umbraco.org/wiki/reference/templates/umbracomacro-element/macro-parameters
hth
Your input is an attribute (at least your xml example suggests this) so your xpath should look like this:
<xsl:param name="mediaFolderId" select="/macro/#mediaFolderId" />
<xsl:param name="title" select="/macro/#title" />

In XSLT/XPath, how do I get the first attribute that doesn't match a specific name?

I have this snippet of XSLT code:
<xsl:variable name="key" select="#*[1]"/>
But I need it to actually set key to be the first attribute (if it exists) that doesn't have a special name. So both of these nodes would have the same key, and special_attr is ignored if it exists.
<MyNode var="1" />
<MyNode special_attr="foo" var="1" />
What is the proper select syntax to get the first attribute's value whose name isn't special_attr?
<xsl:variable name="key" select="#*[not(name() = 'special_attr')][1]"/>

How do I pass a parameter from ASP to XSL?

I'm writing a simple asp page to show a team rota based on some XML. Here's the XML:
<rota>
<shift date="20091201" primary="Chan" secondary="John" notes="notes"></shift>
<shift date="20091202" primary="Mike" secondary="Alex" notes="notes"></shift>
<shift date="20091203" primary="Ross" secondary="Mike" notes="notes"></shift>
<shift date="20091204" primary="Neil" secondary="Ross" notes="notes"></shift>
</rota>
I want my asp page to show today's rota details and maybe the details for the next couple of days. Since later on I'd like to be able to set a day in the future to see who's working around then I want to be able to pass a YYYYMMDD date from the ASP to the XSL that processes the XML.
Here's the XSL I have so far, just highlighting the hardcoded 'current' date for now:
<xsl:template match="rota">
<html>
<head>
<title>Team Rota</title>
<LINK type="text/css" rel="stylesheet" href="http://www.csfb.net/homepage/global/scripts/csfb_intranet.css"/>
</head>
<body>
<table border="1">
<TR><TH>Date</TH><TH>Primary</TH><TH>Secondary</TH><TH>Notes</TH></TR>
<xsl:for-each select="shift">
<tr>
<xsl:choose>
<xsl:when test="#date = '20091203'">
<td bgcolor='FFF0F0'><xsl:value-of select="#date"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="#date"/></td>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select="#primary"/></td>
<td><xsl:value-of select="#secondary"/></td>
<td><xsl:value-of select="#notes"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
and here's the ASP, not yet passing any parameters:
''// Load the XML
sourcefile = "rota.xml"
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.load(sourceFile)
''// Load the XSL
styleFile = Server.MapPath("rota.xsl")
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)
htmltext = source.transformNode(style)
Response.Write htmltext
So how do I a) pass a parameter to the XSL and b) pick up that parameter and use it in the XSL?
Thanks for any guidance.
In you xsl create a xsl:parameter at the top before all your templates.
<xsl:parameter name="showdate"/>
You can now treat this like you would a variable:-
<xsl:when test="#date = $showdate">
In order to pass a parameter in you need to use an XSL processor object which allows you to add the parameter before processing. In turn you get a processor from an instance of an XSL Template object. In turn you need a FreeThreadedDOMDocument to assign to the templates stylesheet parameter. Hence the code is more complex:-
Dim xsl : Set xsl = CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
xsl.async = false
xsl.load xslFile
Dim xml : Set xml = CreateObject("MSXML2.DOMDocument.3.0")
xml.async = false
xml.load sourceFile
Dim xslTemplate : Set xslTemplate = CreateObject("MSXML2.XSLTemplate.3.0")
xslTemplate.stylesheet = xsl;
Dim processor : Set processor = xslTemplate.createProcessor()
processor.addParameter "showDate", "20091203"
processor.input = source
processor.transform()
Response.Write processor.output
Its tempting to assign the Response object to the processor's output parameter which works quite well and is more efficient. However recent Service packs of MSXML have made such a technique incompatible with ASP's implementation of IStream in the Response object.
So thats how you do it officially, how I do it normally is to inject some arbitary attribute on to the source XML's root node then use a variable:-
xml.documentElement.setAttribute("_showDate", "20091203")
Now you can use a variable instead of a parameter inside your main template:-
<xsl:template match="rota">
<xsl:variable name="showdate" select="#_showDate" />
<html> ...
<xsl:when test="#date = $showdate">
In this approach you can use the transform code you are already using which is much simpler.
You should declare your parameter like this:
<xsl:stylesheet ... >
<xsl:parameter name="your_parameter"/>
<xsl:template match="rota">
$your_parameter = <xsl:value-of select="$your_parameter" />
...
And pass it like this
'// Load the XSL
styleFile = Server.MapPath("rota.xsl")
set style = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
style.async = false
style.load(styleFile)
Set template = Server.CreateObject("MSXML2.XSLTemplate.3.0")
template.stylesheet = style
Set processor = myTemplate.createProcessor()
processor.addParameter "your_parameter", "value"
processor.input = source
processor.output = Response
processor.transform()