How can I send a node (any) to an xsl:function?

xml, xslt

Solution

I think the answer to your question is, yes. You can send a node to an XSLT function.

If you are wondering what to use for the value of the as="" attribute, you have several choices. If you want to be very lax and accept just about anything, use as="item()*".

From David Pawson's site:

item()* .. sort of nodeset? W3C

Yes, I agree it looks pretty meaningless doesn't it. However. As of CR, its pretty essential, especially if you want to use types. And want to generate, say, a nodeset.. sorry sequence, in a variable.

<xsl:variable name="a"
 select="(//h3)[position() < 3]"
 as="item()*"/>

This creates a variable you can hack into using xpath quite readily. I.e. remember item()*.

types ... a few examples. W3C

From an explanatory email from Mike Kay, thanks Mike. Examples:

`<xsl:param name="x" as="item()"/>`

the parameter value can be any item (i.e. a node or atomic value). But it must be a single item.

`<xsl:param name="x" as="item()?"/>`

the parameter can be a single item or an empty sequence

`<xsl:param name="x" as="item()+"/>`

the parameter must be a sequence of one or more items - an empty sequence is not allowed

`<xsl:param name="x" as="item()*"/>`

the parameter can be any sequence of zero or more items - this places no constraints on its value.

`<xsl:param name="x" as="node()*"/>`

the parameter can be any sequence of zero or more nodes

`<xsl:param name="x" as="xs:atomicValue*"/>`

the parameter can be any sequence of zero or more atomic values (e.g. integers, strings, or booleans).

item()* is the most general type possible, it matches everything, like "Object" in Java. For that reason, it can usually be omitted. But not always, for example the default type in xsl:variable is not item()* but document-node(), to ensure that

`<xsl:variable name="rtf"><a>thing</a> </xsl:variable>`

continues to behave like XSLT 1.0

Use these to specify parameters, variable types etc.

Problem

Can I send a node to an XSLT function? For example: ``` <books> <book> <author>a1</author> <price>10</price> <year>2009</year> </book> <!-- ... --> </books> ``` Can I send the `<book>` element to a function - within that function I want to process the nodes under book (`<author>`, `<price>` or `<year>`) Can I create a xsl:function as below ? ``` <xsl:function name="util:checkNode" as="xs:boolean"> <!-- I would like to know xml schema data type for the param --> <xsl:param name="nodeP" as="****"/> </xsl:function If yes, what xsl schema type to the param ? ``` It looks like i created lot of confusion to every one by saying function instead of xsl:function ---- :(

Original source