default parameter values

Parameters

1. Default value for parameters
2. Accessing parameters which contain markup
3. How to output the current users login and full name
4. Passing top level param
5. Using stylesheet parameters
6. Can I pass a set of nodes as a parameter to a template

1.

Default value for parameters

David Carlisle


>I can't figure out what
>  <xsl:param name="data" select="self::node()[false()]"/>
>  could be doing; is supposed to do; does it make any sense?

It is declaring the parameter and giving it a default value of an empty node set. Yes it does make sense, I usually use "/.." for that but the above would do.

If you expect a param to be passed in as a node set then if you leave the default being <xsl:param name="data" /> it will be an empty string and things like select="$param/a/b" would be a type error.

However if you make the default an empty node set then these expressions are legal but select nothing, which may be what is required.

2.

Accessing parameters which contain markup

Kay Michael


> I have the following template rule
> 
> <xsl:template name="separated-list">
> 	<xsl:param name="nodes"/>
> 	<xsl:param name="separator"/>
> 	<xsl:for-each select="$nodes">
> 		<xsl:value-of select="."/>
> 		<xsl:if test="position() != last()">
> 			<xsl:value-of select="$separator"/>
> 		</xsl:if>
> 	</xsl:for-each>
> </xsl:template>
> 
> Sometimes separator is "," and other times it's a <BR>
	

If the separator is a node-set containing a single <BR/> element, then <xsl:value-of> will output nothing (the string value of an empty element). Try using <xsl:copy-of> instead.

3.

How to output the current users login and full name

Norm Walsh

A possibility is to construct a little wrapper XSL stylesheet that defines these as variables. Instead of processing the documents with stylesheet x.xsl, construct y.xsl:

<?xml version='1.0'?>
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Tranform"     
	version="1.0">

<xsl:variable name="login-name">ndw</xsl:variable>
<xsl:variable name="full-name">Norman Walsh</xsl:variable>

<xsl:include href="x.xsl"/>
</xsl:stylesheet>

And refer to the variables login-name and full-name in your x.xsl stylesheet.

4.

Passing top level param

Eric van der Vlist


> 
> The XSL Transformation v1.0 spec says about passing top level params:
> "XSLT does not define the mechanism by which parameters are passed to the
> stylesheet"
> 
> Does anyone now if any xslt processors does this?

XT does... Quoting James C : java -Dcom.jclark.xsl.sax.parser=your-sax-driver com.jclark.xsl.sax.Driver source stylesheet result name=value... The name=value arguments are optional and specify parameter names and values; they can occur in any order with respect to the other arguments. They will be ignored unless the stylesheet contains a corresponding top-level xsl:param element. The value of the parameter will be of type string.

Michel CASABIANCA gives an example

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>
  <xsl:param name="param">default</xsl:param>

  <xsl:template match="/">
    <xsl:call-template name="test"/>
  </xsl:template>

  <xsl:template name="test">
    <xsl:value-of select="$param"/>
  </xsl:template>
</xsl:stylesheet>

called with any xml file like this : java com.jclark.xsl.sax.Driver test.xml test.xsl param=value Will output "value" rather than default.

5.

Using stylesheet parameters

Jeni Tennison

To use parameters passed in from an asp page, you have to: 1. pass the parameters from the asp page 2. accept the parameters within the stylesheet 3. use the parameters within the stylesheet

I'm not sure which of these stages you're having problems with. Ben assumed you were having problems with stage 1, which is passing the parameters into the stylesheet from the asp page. When he talked about the parser you were using, he meant what are you using to make the stylesheet run, to call the stylesheet from the asp page. It sounds as though it's MSXML, which is Microsoft's XML/XSLT processor - you should make sure you have the most recent version from their web site. Ben offered to help you more with the asp side of things, and I don't know much about it, so I'll concentrate on giving you a hand with the other two stages.

For Stage 2, accepting the parameters within the stylesheet, you need to declare them using xsl:param elements at the top level of the stylesheet (immediate children of the xsl:stylesheet element):

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ...
  <xsl:param name="foo" />
  ...
</xsl:stylesheet>

You can specify a default value for the parameter either as its content or as the value of the 'select' attribute, if you want. Usually stylesheet parameter values are strings, so be aware, if you use the 'select' attribute, that you have to put quotes around the default value, otherwise it will interpret it as an XPath, so use:

  <xsl:param name="foo" select="'bar'" />

rather than:

  <xsl:param name="foo" select="bar" />

which will try to set it to the value of any 'bar' elements that are lying around.

As far as using the parameter is concerned (Stage 3), you can refer to a particular parameter using '$parameter-name', so '$foo' in the case above. If you want to test its value to see what to generate, for example, you can use:

  <xsl:if test="$foo = 'bar'">
    <!-- some content -->
  </xsl:if>

6.

Can I pass a set of nodes as a parameter to a template

Jeni Tennison

Yes, but "node sets" in XSLT terminology are sets of nodes from the source document, not sets of nodes that you generate within the stylesheet. You can make a parameter or variable take a node set as a value by setting that value with the 'select' attribute.

When you set a parameter or variable value using the content of the parameter or variable, then you create a "result tree fragment", which is exactly what it says on the tin - a fragment of the result tree. Result tree fragments aren't the same as source nodes, and you can do only limited things with them. One of the things that you *can't* do is process them in any way.

Some processors have extension functions that allow you to convert result tree fragments into node sets. xt:node-set() saxon:node-set() xalan:node-set()

Without using an extension function, you may have to do two passes: one pass creating a result that you then use as the input to the next pass.