xsl-list
[Top] [All Lists]

Re: parameters - element vs attributes

2004-06-03 08:09:09
Hey Stephen,

First off, welcome to the wonderful world of XSLT.  As with adding any new 
language to your resource pool there are going to be some aches and pains in 
learning new syntax, data types, and, as in the case of XSLT, probably a 
brand new paradigm.  While most of the main stream languages like C++, Java, 
VB, C#, etc... use a procedural style syntax XSLT uses a declarative (you 
may also here it referred to as a functional language) syntax.  Recursion is 
a gigantic part of this style/format and you will find that becoming 
intimate with recursive principles will help you immensely in your quest to 
master XSLT.  And while your still young and impressionable (as far as your 
experience with XSLT is concerned) I would also encourage you to add the 
following links to your favorites folder:

http://www.w3.org/TR/xslt and http://www.w3.org/TR/xpath for your default 
online references/specifications. I would read through both of these the 
best I could...  they will be difficult to understand at first but you are 
more likely to be able to recall the spot in the spec that contains the 
answer to your future question if you spend the time to read through them 
now and give yourself something for your brain to latch onto.

I would add Dave Pawsons XSL FAQ link 
http://www.dpawson.co.uk/xsl/index.html as well as Jeni Tennisons XSLT Pages 
http://www.jenitennison.com/xslt/index.html
Both are excellent, well maintained and updated resources for the novice and 
expert XSLT/XPath developer alike.

To round things out and to act as your mentoring guide I would invest the 
CDN$37.09 into Michaels Kays XSLT: Programmer's Reference, 2nd Edition (don't 
confuse that will the XSLT 2.0 version which is not released but can still 
be pre-ordered from Amazon.  The 1.0 version is what you need and can be 
found on Amazon.CA at 
http://www.amazon.ca/exec/obidos/ASIN/0764543814/qid=1086273254/sr=1-1/ref=sr_1_0_1/702-7295360-3845622

Ok, now that you've been given a good base of soil and fertilizer and then 
freshly watered your ready to take root.  Have I scared you away yet or are 
you still here with me?  Still here! GREAT!  Now to your problem...

Two things...

<xsl:with-param name="theLinkGroup" select="'siteLinkList'"/>

The above parameter selects the string value "'siteLinkList'" and not the 
element and underlying children "siteLinkList".  The single quotes are what 
tell the processor to treat this as a literal string.  Without them it sees 
an element.  With, a string.


<h2><xsl:value-of select="$theLinkGroup" /></h2> <!-- does work -->

This works because you are simply using xsl:value-of to output the value of 
the param $theLinkGroup which is the string literal 'siteLinkList' passed 
from your previous template.

<xsl:for-each select="//$theLinkGroup/link"> <!-- won't work -->

Definitely not.  Two reasons: 1) to access all link elements contained in 
the $theLinkGroup param (which in current state don't exist because of the 
string issue from above) you would need to use $theLinkGroup//link.  By 
prefacing the $theLinkGroup param with // you are telling the processor to 
access all the elements in the XML document flow called $theLinkGroup. 
There obviously aren't any and in fact because $theLinkGroup is an invalid 
QName you are seeing the error "NodeTest" expected here.  $theLinkGroup 
isn't a node, it's a param, and so the test fails immediately. You need to 
use valid elements and/or attributes that conform to the QName definition to 
properly select the Result Tree Fragment that you desire.  And reason 2) as 
already mentioned the value of $theLinkGroup is currently a string and 
therefore has no underlying link descendants to be processes in your 
xsl:for-each loop.

I hope all of this helps you to get where you need to be.  Don't hesitate to 
ask questions whenever you need the help.  But you will find that a little 
homework on your part will go a long ways in both helping you better 
understand the help you are given as well as showing encouraging signs to 
the list members that you mean business and cause us to want to help you out 
even more.

Best of luck Stephen!

<M:D/>




----- Original Message ----- 
From: <stephen(_dot_)peterson(_at_)mnr(_dot_)gov(_dot_)on(_dot_)ca>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Thursday, June 03, 2004 8:12 AM
Subject: [xsl] parameters - element vs attributes


Hi there,
I'm kind of new to XML and I'm trying to figure out how I can use a
parameter (theLinkGroup) to select an element when used in another 
template.
I have used it (a parameter in another template) to select an element 
based
on the value of it's attribute before. But I keep getting a error about
"NodeTest expected here" and showing me the beginning of my select
statement.

In the past I have used a parameter called "themeSubject" and used it in
another template to select an element with the attribute having the smae
value of the parameter. An it worked fine. <e.g.> <xsl:with-param
name="themeSubject" select="'GIS'"/> ... in other template ... 
<xsl:for-each
select="//siteLinkList/link[themeType/item=$themeSubject]">
</e.g.>

Can someone point me to a resource that will explain this to me? I know
there are some restrictions on what you can do with parameters and paths.

Many thanks,
MNR Steve


Here is most of my current code....

<xsl:template match="/">
   <xsl:call-template name="siteLinks">
     <xsl:with-param name="theLinkGroup" select="'siteLinkList'"/>  <!--
parameter
   </xsl:call-template>
</xsl:template>

<!-- other templates -->
<!-- other templates -->

<xsl:template name="siteLinks">
<xsl:param name="theLinkGroup"></xsl:param>
 <h2><xsl:value-of select="$theLinkGroup" /></h2>           <!----- does
work
  <ul class="xsmall">
  <xsl:for-each select="//$theLinkGroup/link">                   <!-----
won't work
   <li><a>
    <xsl:attribute name="href"><xsl:value-of 
select="url"/></xsl:attribute>
     <xsl:value-of select="@title"/>
    </a></li>
  </xsl:for-each>
  </ul>
</xsl:template>

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--



<Prev in Thread] Current Thread [Next in Thread>