This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Global Variables (use of)


Jason,

<xsl:variable name="sectnum">
  <xsl:value-of select="child::title/attribute::id"/>
</xsl:variable>

Let's go through what this variable declaration is doing so that you can
understand why it isn't working.

First, look at the value that you're trying to give to the variable.  The
variable is being defined at the top level, which means that the current
node is the 'root node' of the input (the node representing the input
document).  The current node is used when interpreting XPaths - your XPath
is thus retrieving the 'id' attributes of the 'title' children of the *root
node*.  From the sounds of it, your document element isn't a 'title'
element, so this XPath won't find any 'id' attributes.

What you were aiming to do, I think, was find any 'id' attribute of a
'title' element that is a child of a any element within your input
document.  To do that, you need an XPath that looks something like:

  //child::title/attribute::id

or, more simply:

  //title/@id

However, when you give a variable a value by putting it as the content of
the xsl:variable element, the value is interpreted as a 'result tree
fragment', which essentially means that you can't process that information
any more (unless you use an extension function).  With the variable
definition:

<xsl:variable name="sectnum">
  <xsl:value-of select="//title/@id"/>
</xsl:variable>

you will just get a concatenated string of ids, which is probably next to
useless.  If you want to do any kind of processing on a variable, then you
should specify its value using the 'select' attribute instead.  For example:

<xsl:variable name="sectnum" select="//title/@id" />

will set the variable $sectnum to be a node list of all the 'id' attribute
nodes of 'title' elements within your document.  You could then iterate
over them to give your table of contents.

Having said all that, I'm not sure this is going to be a good approach
given your description of what you're after.  What is wrong with something
along the lines of:

<xsl:template match="/">
  ...
  <!-- this does the table of contents -->
  <xsl:apply-templates mode="TOC" select="//*[title]">
  <!-- this does the body of the book -->
  <xsl:apply-templates />
  ...
</xsl:template>

<xsl:template mode="TOC" match="*[title]">
  <!-- link to the anchor with the id of the 'id' attribute of the 'title'
       child element -->
  <a href="#{title/@id}">
    <xsl:value-of select="title" />
  </a>
</xsl:template>

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@epistemics.co.uk


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]