Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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 ---- :(

like image 716
Laxmikanth Samudrala Avatar asked Aug 28 '09 04:08

Laxmikanth Samudrala


People also ask

What does node () do in XSLT?

XSLT current() Function Usually the current node and the context node are the same. However, there is one difference. Look at the following XPath expression: "catalog/cd". This expression selects the <catalog> child nodes of the current node, and then it selects the <cd> child nodes of the <catalog> nodes.

What is node set in XSLT?

exsl:node-set() XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. exsl:node-set() returns a node-set from a result tree fragment, which is what you get when you look at the xsl:variable instead of its select attribute to fetch a variable's value.

How do you assign a value to XSLT variable?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!


1 Answers

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.

like image 164
Mads Hansen Avatar answered Sep 19 '22 14:09

Mads Hansen