Solved

Tunneling in 2.0

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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).

803 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