Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your question:</p> <blockquote> <p>Can I send the &lt;book&gt; element to a function - within that function I want to process the nodes under book (&lt;author&gt;, &lt;price&gt; or &lt;year&gt;)</p> </blockquote> <p>and later comment:</p> <blockquote> <p>can i have a function which accepts the node as a parameter?</p> </blockquote> <p>...you're asking for a function, but what <strong>i think you want is a <a href="http://www.w3schools.com/xsl/el_template.asp" rel="nofollow noreferrer"><code>template</code></a> for the <code>book</code> element</strong>.</p> <p>Of course, you could do this several ways--in a <a href="http://www.w3schools.com/xsl/el_for-each.asp" rel="nofollow noreferrer"><code>for-each</code></a> loop, actually sending the <code>book</code> node into a template using <a href="http://www.w3schools.com/xsl/el_with-param.asp" rel="nofollow noreferrer"><code>with-param</code></a>, etc.--but a simple <a href="http://www.w3schools.com/xsl/el_template.asp" rel="nofollow noreferrer"><code>template</code></a> should accomplish what you need. Whatever the template matches is the "parameter".</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="books"&gt; &lt;xsl:apply-templates select="book"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="book"&gt; author: &lt;xsl:value-of select="author" /&gt; price: &lt;xsl:value-of select="price" /&gt; year: &lt;xsl:value-of select="year" /&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>If you want to do <strong>some further processing</strong> on the sub-elements (author, price, and year), you could look into creating templates for them too. Keep in mind that matching "year" will match <strong>all</strong> year elements, even for those you might have in date of births etc. (In these cases you would probably want to restrict the XPath in the match or just keep the <code>book/year</code> processing within the book template.) Say you want to do something extra with <code>year</code>. Your book processing might evolve into something like this (where you're now passing each <code>year</code> node as parameters into the year template):</p> <pre><code>&lt;xsl:template match="book"&gt; author: &lt;xsl:value-of select="author" /&gt; price: &lt;xsl:value-of select="price" /&gt; &lt;xsl:apply-templates select="year" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="year"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="year &amp;lt; '2000'"&gt; from a prior century: &lt;xsl:value-of select="." /&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; from this century: &lt;xsl:value-of select="." /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload