Solved

Tunneling in 2.0

Posted on 2008-10-01
4
258 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AngularJS directive issue 6 80
Form submit issue 11 89
Having trouble setting Apache to recognise 2 vitual sites (on different drive). 3 163
Fulfillment API php code sample 1 72
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

679 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