Cary,
 
The difference is that inside square brackets, the context node changes. So @e refers to an e attribute of the item in $bar that you're testing (it hasn't got one), whereas if you use the variable, @e is referring to an attribute of the my:element element.
 
If you want to avoid declaring a variable, you can use current()/@e - current() is the node that was current at the outermost level of the XPath expression, ie. outside the square brackets.
 
Michael Kay


From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Cary Millsap
Sent: 10 November 2004 15:51
To: 'Saxon Help List'
Subject: [saxon] Can't use an attribute reference

I’m using Saxon-B 8.1 (by the way, I get a “file not found” error at sourceforge when I try to download 8.1.1).

Can anyone help me understand why the two <line> elements don’t have the same value in the output of the transform below? (You can use any file as input to this transform.)

 

Cary Millsap
Hotsos Enterprises, Ltd.
http://www.hotsos.com
Nullius in verba

Upcoming events:
- Performance Diagnosis 101: 1/4
Calgary, 2/2 Sydney
- SQL Optimization 101: 11/8
Dallas, 12/13 Atlanta, 2/7 Sydney
- Hotsos Symposium 2005: March 6–10
Dallas
- Visit www.hotsos.com for schedule details...

 

 

<?xml version="1.0" encoding="UTF-8"?>

 

<xsl:transform version="2.0"

xmlns="http://www.w3.org/1999/xhtml"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:my="http://www.my.com"

exclude-result-prefixes="xs"

>

       <xsl:output indent="yes"/>

      

       <xsl:variable name="data">

              <my:element e="314.782"/>

       </xsl:variable>

 

       <xsl:template match="/">

              <xsl:variable name="bar" as="element()*">

                     <char>c0</char>

                     <char>c1</char>

                     <char>c2</char>

                     <char>c3</char>

                     <char>c4</char>

                     <char>c5</char>

                     <char>c6</char>

                     <char>c7</char>

                     <char>c8</char>

              </xsl:variable>

              <xsl:variable name="max-height" select="932.108"/>

              <xsl:variable name="i" select="1 + round($data/my:element/@e div $max-height * 8)"/>

              <output>

                     <xsl:for-each select="$data/my:element">

                           <xsl:variable name="e" select="@e"/>

                           <line>$bar[f(@e)] = <xsl:value-of select="$bar[1 + round(@e div $max-height * 8)]"/></line>

                           <line>$bar[f($e)] = <xsl:value-of select="$bar[1 + round($e div $max-height * 8)]"/></line>

                     </xsl:for-each>

              </output>

       </xsl:template>

 

</xsl:transform>