Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Tunneling in 2.0

Posted on 2008-10-01
4
Medium Priority
?
264 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

927 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