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

Can I send an XSLT function to node? For example:

<books> <book> <author>a1</author> <price>10</price> <year>2009</year> </book> <!-- ... --> </books> 

Is it possible to send the <book> element to a function - inside this function I want to process the nodes in the book ( <author> , <price> or <year> )

Is it possible to create an xsl function: 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 a lot of confusion for everyone by saying a function instead of xsl: function ----: (

+6
xml xslt
source share
5 answers

I think the answer to your question is yes. You can send node XSLT functions.

If you are interested in what to use for the value of the as = "attribute, you have several options. If you want to be very weak and accept anything, use as =" item () * " .

From David Pawson's site :

item () * .. kind of node node? W3c

Yes, I agree that this looks pretty pointless, doesn't it. But. Starting with CR, this is pretty important, especially if you want to use types. And I want to generate, say, a set of nodes .. sorry sequences in the variable.

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

This creates a variable that you can hack into using xpath quite easily. That is, remember the element () *.

types ... a few examples. W3c

From Mike Kay's explanatory letter, thanks to Mike. Examples:

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

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

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

the parameter may be a single element or an empty sequence

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

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

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

a parameter can be any sequence of zero or more objects - this does not put a limit 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*"/>

a parameter can be any sequence of zero or more atomic values ​​(for example, integers, strings, or logical values).

item () * is the most common type possible, it matches everything, like an "Object" in Java. For this reason, it can usually be omitted. But there is not always, for example, the default type in xsl: the variable is not an element () *, but document-node (), to make sure

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

continues to behave like XSLT 1.0

Use them to specify parameters, variable types, etc.

+8
source share

XSL doesn't care what a parameter is. It really treats everything as XML. If the text is passed in it, it is treated as text node.

Here is an example with a link for further reference.

http://www.w3schools.com/xsl/

 <xsl:call-template name="TemplateName"> <xsl:with-param name="ParamName" select="MyNode/MyNode" /> </xsl:call-template> <xsl:template name="TemplateName"> <xsl:param name="ParamName" /> </xsl:template> 
0
source share

There are many XSLT functions that take nodes as parameters. For example, all concat arguments can be nodes, for example:

 concat(myelement, myelement/@myattribute) 

While this answers exactly the question you asked, I would argue, adding up money, that this is not a question that you would like to answer. But hell, you asked him four times, so you're here.

Edit

In fact, attributes are not nodes; only elements, text, comments, processing instructions, and CDATA sections are nodes. But the above example still answers the question correctly.

0
source share

To add an answer to ChaosPandion , you may need to add node test to your XPath request:

 <xsl:call-template name="TemplateName"> <xsl:with-param name="ParamName" select="MyNode/MyNode/node()" /> </xsl:call-template> <xsl:template name="TemplateName"> <xsl:param name="ParamName" /> </xsl:template> 

Officially, node() not a function, it is a test, but it can return a value similar to the XmlNode.InnerXml property.

-one
source share

In your question:

Can I send an <book> element to a function - inside this function I want to process nodes under the book (<author>, <price> or <year>)

and later comment:

Can I get a function that takes node as a parameter?

... you are requesting a function, but what , I think, you want a template for the book element .

Of course, you could do this in several ways - in the for-each , by actually sending the book node to the template using with-param , etc., but a simple template should do what you need. Whatever the template, this is a "parameter".

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="books"> <xsl:apply-templates select="book"/> </xsl:template> <xsl:template match="book"> author: <xsl:value-of select="author" /> price: <xsl:value-of select="price" /> year: <xsl:value-of select="year" /> </xsl:template> </xsl:stylesheet> 

If you want to do some additional processing for sub-elements (author, price, and year), you can also examine them to create templates. Keep in mind that matching "year" will match all year elements, even for those that you may have in your date of birth, etc. (In these cases, you probably want to limit XPath to the match, or just save the book/year processing in the book template.) Say you want to do something extra with year . Your book processing might turn into something like this (where you now pass each year node as parameters to the year template):

 <xsl:template match="book"> author: <xsl:value-of select="author" /> price: <xsl:value-of select="price" /> <xsl:apply-templates select="year" /> </xsl:template> <xsl:template match="year"> <xsl:choose> <xsl:when test="year &lt; '2000'"> from a prior century: <xsl:value-of select="." /> </xsl:when> <xsl:otherwise> from this century: <xsl:value-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:template> 
-one
source share

All Articles