Solved

Tunneling in 2.0

Posted on 2008-10-01
4
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

Suggested Solutions

Title # Comments Views Activity
PHP parse error 7 65
RSS Feed Validation 2 80
How to silent print from safari browser 6 324
AngularJS directive issue 6 86
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

751 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