[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

[XSL] Implicit Predicate Casting

Subject: [XSL] Implicit Predicate Casting
From: Alain <alainb06@xxxxxxx>
Date: Sat, 13 Oct 2007 15:49:37 +0200
 [XSL] Implicit Predicate Casting
Hello XSL-List members,

after having been trapped myself with the "empty-sequence test" {eq. () != 'anything not empty sequence' is false }, I now understand fully this trick, with the help of examples in this list, and even found the paragraphs of the W3C norm where it is specified.

But there is still something that puzzles me a lot, and I still don't find the part that specifies the behaviour described below.

Let's consider :

XML input :
===========================
<?xml version="1.0" encoding="UTF-8"?>
<test>
   <element>first</element>
   <element>second</element>
   <element>third</element>
 <empty/>
</test>

===========================


XSL file : =========================== <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">

<xsl:output method="html" encoding="utf-8"/>

 <xsl:template match="/">
     <html>
         <head><title>Test</title></head>
         <body>
             <xsl:apply-templates select="test"/>
         </body>
     </html>
 </xsl:template>

   <xsl:template match="test">
       <p>(1) element[2]=<xsl:value-of select="element[2]"/></p>

<xsl:variable name="pos">2</xsl:variable>
<p>(2) element[$pos]=<xsl:value-of select="element[$pos]"/></p> <p>(3) element['2']=<xsl:value-of select="element['2']"/></p> <p>(4) element[number($pos)]=<xsl:value-of select="element[number($pos)]"/></p> <p>(5) element[position()=$pos]=<xsl:value-of select="element[position()=$pos]"/></p>


<xsl:variable name="pos-int" select="2"/>
<p>(6) element[$pos-int]=<xsl:value-of select="element[$pos-int]"/></p>


<xsl:variable name="pos-cast" as="xs:integer">2</xsl:variable>
<p>(7) element[$pos-cast]=<xsl:value-of select="element[$pos-cast]"/></p>
</xsl:template>


</xsl:stylesheet>
===========================

Ouput is (using Saxon 8.9)
===========================
<html>
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <title>Test</title>
  </head>
  <body>
     <p>(1) element[2]=second</p>
     <p>(2) element[$pos]=first second third</p>
     <p>(3) element['2']=first second third</p>
     <p>(4) element[number($pos)]=second</p>
     <p>(5) element[position()=$pos]=second</p>
     <p>(6) element[$pos-int]=second</p>
     <p>(7) element[$pos-cast]=second</p>
  </body>
</html>
===========================



(1) element[2] would implicitly mean element[position()=2], and value-of is "stringifying" the result sequence on 1 node (in position 2 of element list)

On (2) and (3), the predicate expression (inside []) is a string (3) or a variable (2) containing a string (or is it considered as a nodeset containing a single text node under the root element of the variable ?).
It's obviously not trying to cast to a number because that gives a different result as (4) where I cast it to a number.


It does not seem to try to match it to a position(), because on (5) when I explicitly test for position it gives the same result as (1).

And (6) and (7) when the variable is of type integer, either implicitly or by casting it to integer, it gives the same result as (1).


*So my question is* : what is happening in (2) and (3) at XPath level to get this result ?


And also, why is the result different if the version of XSL is set 1.0. It would then give only
<p>(2) element[$pos]=first</p>
<p>(3) element['2']=first</p>


(looks like stringify-first-element-of-the-sequence thing)
... it's another "tricky" thing of XSL/XPath I would like to understand !



And by the way, I'm very newbie with xsl:fallback and stuff like that, is there a way I can run this stylesheet with Internet Explorer without having to comment out the last two lines of the template, because I.E. complains about the attribute "as" of xsl:variable which, indeed, does not exist in 1.0 ?
(so that's the third question !)
... or maybe it's a bug of I.E. that should not complain with this attribute "as" and just ignore it, as Firefox does... or is this behaviour "implementation dependant" ?


(Of course, when I tried on browsers, I changed version of XSL to 1.0)

Many thanks to all, this list is very helpful and I like reading it's morning digest.

Alain BENEDETTI

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.