Closed Bug 370784 Opened 19 years ago Closed 19 years ago

testing xpath position within an xsl template

Categories

(Core :: XSLT, defect)

1.8 Branch
x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: gollum.greg, Unassigned)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1 In an xsl transform, when attempting to set a html div style...class="last", I found that position() tests fail - though IE engine understands them. is this a (known?) ff bug or does IE get it wrong? - many hours of *newbie* head-scratching. Reproducible: Always Steps to Reproduce: 1.<apply templates select="item"> 2.<template match="item"> 3.<if/when test="position() mod 2=1> 4.<if/when test="position()=last()> Actual Results: <div class="item"> Expected Results: <div class="item last"> or <div class="item odd"> etc. Ended up moving the tests to template conditions. i.e. <xsl:template match="item[position() mod 2=1]"> or <xsl:template match="item[postion()=last()]">
Assignee: nobody → xslt
Component: General → XSLT
Product: Firefox → Core
QA Contact: general → keith
Version: unspecified → 1.8 Branch
Please attach a minimal small testcase. IE skips textnodes contaning only whitespace which could be the problem here (IE is violating the spec by doing so)
(In reply to comment #1) > Please attach a minimal small testcase. IE skips textnodes contaning only > whitespace which could be the problem here (IE is violating the spec by doing > so) > testcase.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="testcase.xsl"?> <testcase> <item>One</item> <item>Two</item> <item>Three</item> <item>Four</item> <item>Five</item> </testcase> testcase.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="testcase"> <html> <head><title>TestPosition</title> <style type="text/css">.last {color:red;}</style></head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="item"> <div> <xsl:if test="position()=last()"> <xsl:attribute name="class">last</xsl:attribute> </xsl:if> <xsl:apply-templates/> </div> </xsl:template> </xsl:stylesheet>
First of all, please attach the testcase rather than paste it inline. <xsl:apply-templates/> is shorthand for <xsl:apply-templates select="node()"/> which will select both the <item> elements and the whitespace nodes between them. So we are doing the right thing here since the node for which position()=last() is a whitespace textnode which naturally doesn't match the template. To fix this change to <xsl:apply-templates select="item"/>
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
Sorry Jonas for the inline post but I'm unable to post to a public server. The concept of whitespace nodes between elements is brand new to me, thank you. This unexpected behavior has been damn frustrating. What makes it worse is I've had advice from more than one forum that IE was correct and FF was somehow (though it was never explained how) in error. I constantly (and consistently) coded around your explanation. Again, thank you! My apologies for wasting your, and everyone else's, valuable time.
You need to log in before you can comment on or make changes to this bug.