Solved

Tunneling in 2.0

Posted on 2008-10-01
4
254 Views
Last Modified: 2013-11-18
Geert helped me with a previous issue (ref: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XSLT/Q_23702539.html). I am having problems understanding "tunneling" in the aforementioned question.
0
Comment
Question by:kmartin7
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22614754
with apply-templates you can pass a parameter (xsl:with param)
(just the same way as you can do with call-templates)
All you need is an empty parameter declaration in the applied template

In XSLT1, this would work fine, if you would pick up this parameter on each level
and pass it on to the next level

This is clumsy since that would mean that you need a template for each layer in your XML

IN XSLT2 you can tunnel a parameter to any level as long as you set the tunnel="ye" in both the with-param, as in the <xsl:param> in the applied template. It saves you from having this intermediate template that picks up and passes along.
I will send an example in a minute
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22615061
Look at this XML
<?xml version="1.0" encoding="UTF-8"?>
<table bar="foo">
    <row>
        <cell/>
    </row>
</table>
What I will try to do is pass the attribute value to the template for cell
(you could pick this up with ancestor::table/@bar, but just for the sake of the example, I want to avoid having to look up the ancestor axis)

This XSLT1 stylesheet will do that for me (don't use Saxon9B for this example, since it tunnels this, even in XSLT1 mode)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="table">
        <xsl:apply-templates>
            <xsl:with-param name="str" select="@bar"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="row">
        <xsl:param name="str"/>
        <xsl:apply-templates>
            <xsl:with-param name="str" select="$str"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="cell">
        <xsl:param name="str"/>
        <xsl:text>[</xsl:text>
        <xsl:value-of select="$str"/>
        <xsl:text>]</xsl:text>
    </xsl:template>
</xsl:stylesheet>

You will see
[foo]
in the output
but if you drop the row template, you will find that the built-in template for elements will NOT pass the parameter to the next level.
and you will see
[]
This is very clumsy, since if I want to pass the attribute to the cell level, I need to make sure that I have a parameter passing template for every intermediate level

Now, in XSLT2, I can mandate a with-param to tunnel a particular parameter through every layer in between
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="table">
        <xsl:apply-templates>
            <xsl:with-param name="str" select="@bar" tunnel="yes" />
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="cell">
        <xsl:param name="str" tunnel="yes"/>
        <xsl:text>[</xsl:text>
        <xsl:value-of select="$str"/>
        <xsl:text>]</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Drawing up this example, I noticed that Saxon9B seems to tunnel if you don't have the attribute at all.
In my opinion that is a mistake, and you should not rely on this behaviour of Saxon

0
 
LVL 11

Author Closing Comment

by:kmartin7
ID: 31501987
Thank you!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22616782
welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now