Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2008, 06:36 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default TREE / HIERARCHY

Hi,

I am struggling to try and understand what this xslt is doing and would appreciate some help in understand its logic.

Here is the XML:
________________

<?xml version="1.0"?>
<Label ID="gb03spp" xmlns="">
  <C CR="RADIO" CID="555" CPID="48" CC="39">
    <S SR="RADIO SHOWS" SID="5419" SPID="555" SC="7">
      <T TR=" " TID="12101" TPID="5419" TRID="TTTTTTTT" TC="1">
        <Q QR="Pepsi Network Chart Show" QID="75928" QPID="12101" QC="0">
          <A AR="I Specially Choose To Listen To It" AID="766804" APID="75928" AC="5"/>
          <A AR="I Listen To It Only Because Someone Else Likes It" AID="766805" APID="75928" AC="5"/>
        </Q>
        <Q QR="I Specially Choose To Listen To It" QID="75963" QPID="12101" QC="0">
          <A AR="Pepsi Network Chart Show" AID="766993" APID="75963" AC="49"/>
        </Q>
      </T>
    </S>
  </C>
  <C CR="TELEVISION VIEWING" CID="556" CPID="48" CC="39">
    <S SR="TELEVISION PROGRAMMES" SID="5426" SPID="556" SC="7">
      <T TR="TV Programmes" TID="12118" TPID="5426" TRID="TTTTTTTT" TC="1">
        <Q QR="The Pepsi Chart (January-September 2002)" QID="76158" QPID="12118" QC="0">
          <A AR="I Specially Choose To Watch It" AID="768561" APID="76158" AC="5"/>
        </Q>
      </T>
    </S>
  </C>
  </Label>


XSLT
____

<xsl:template match="/" >
    <div class="show">
      <xsl:call-template name="h">
        <xsl:with-param name="path">
          <xsl:value-of select="/Label/@dirroot" />
        </xsl:with-param>
      </xsl:call-template>
    </div>
  </xsl:template>

From the root, we call templates and pass the $path parameter. As you can see in the XML above, there is no /Label/@dirroot attribute in the xml - <xsl:value-of select="/Label/@dirroot" />. So in the dubugger I see $path {Dimension:[1]} [1]{Children:[0]}

<xsl:template name="h" match="/Label" >
    <xsl:param name="path" />
    <div class="show">
      <xsl:attribute name="id">
        <xsl:value-of select="@HeaderID" />
      </xsl:attribute>
      <xsl:attribute name="hfs">
        <xsl:if test="following-sibling::*">true</xsl:if>
      </xsl:attribute>
      <xsl:apply-templates>
        <xsl:with-param name="path">
          <xsl:value-of select="$path" />\<xsl:value-of select="@name" />
        </xsl:with-param>
      </xsl:apply-templates>
    </div>
  </xsl:template>

Here we recursively apply template and path will look like this:

$path {Dimension:[1]} [1]{Children:[1]}child::node()[1] \
What are the two double slashes? How did they appear here? \
We call template again and this time we go to the C template. Inside the C template $path looks like this:

$path {Dimension:[1]} [1]{Children:[1]}child::node()[1] \\\
So it seems that it keeps increment the number of back slashes from \\ to \\\\ and so on.


<xsl:template match="C" >
    <xsl:param name="path" />
    some contente...
    <xsl:apply-templates>
      <xsl:with-param name="path">
        <xsl:value-of select="$path" />\<xsl:value-of select="@name" />
      </xsl:with-param>
    </xsl:apply-templates>
  </div>
</xsl:template>

I would appreciate if someone could help me to understand what is going on here. Is this a common technique used in xslt to build trees where you have a hierarchy? I do not undertand the job of the $path parameters. What are those back slashes \\\\?

Cheers

C
 
Old June 13th, 2008, 06:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, I can't tell why the stylesheet is selecting an attribute that doesn't exist. But given that it does so, it's fairly obvious why it's behaving the way that it does. The backslash characters come from this:

<xsl:value-of select="$path" />\<xsl:value-of select="@name" />

If both $path and @name are empty, then all you are left with is the "\" in between; next time through it adds another "\", and so on.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 13th, 2008, 07:27 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for the reply.

So this might be a technique to work with a hierarchy? Maybe the backslash tells the position we are on in the hierarchy?

The xslt works fine but why someone would use an empty value for a parameter like that? It is mind boggling.

Cheers

C
 
Old June 13th, 2008, 07:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>why someone would use an empty value for a parameter like that?

I'm a programmer, not an archeologist. When I see something like this I tend to assume that it was code that did something useful at the time but has been overtaken by events.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search A Hierarchy Tree for Node where id=value sdaspen XSLT 4 February 2nd, 2007 06:34 PM
Hierarchy Navigation asearle XSLT 0 September 28th, 2006 02:27 AM
help with class hierarchy wu4 XSLT 0 October 17th, 2005 01:58 PM
Namespace Hierarchy projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 6th, 2003 09:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.