Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2007, 05:58 AM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is xsl:if syntax sugar?

a bit of theory after all the working class problems ;)

is xsl:if syntax sugar? can you replace
Code:
<xsl:if test="expr">
....
</xsl:if>
with
Code:
<xsl:apply-templates select="." mode="if_template"/>

<xsl:template match="*[expr]" mode="if_template">
....
</xsl:template>
???

is the test expression you can use in the square brackets in match somehow limited?

can you copy the content of xsl:if element to 'if_template' as it is or do you have to modify it(and maybe its context) to keep the semantics?

all this in xslt1.0

 
Old February 21st, 2007, 06:42 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The context for evaluating expr in your original code and in its replacement is different. Consider test="position()=last()".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 07:59 AM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for superfast reply ... BUT

this is exactly why i'm asking. i thought when evaluating expr in if_template the node list contains just a single node(from apply-templates select=".")

but when i test this(with xalan)
Code:
    <xsl:template match="c">
        <xsl:apply-templates select="." mode="if_template" />
    </xsl:template>

    <xsl:template match="*[position()=last()]" mode="if_template">
        <xsl:value-of select="@atr" />
    </xsl:template>

    <xsl:template match="*|/" mode="if_template" />
on this source file
Code:
<a>
    <c atr="c1 atr" />
    <c atr="c2 atr" />
    <c atr="c3 atr" />
</a>
i get this result
Code:
c3 atr
my understanding of xslt is say limited ;) and i expected to see this
Code:
c1 atr
c2 atr
c3 atr
???

 
Old February 21st, 2007, 08:39 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That's exactly the point I was making. The pattern

match="*[position()=last()]"

is defined to match any element that is capable of being selected, from some context node, by the XPath expression select="*[position()=last()]", that is, every element that is the last child of its parent. Namely c3.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 10:47 AM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry, but i still don't get it, even after consulting the xslt specification. there is some fundametal misunderstanding between me and xslt processing model.

can you please explain, where do i do the mistake in my assumptions? this is the stylesheet from above, this time with line numbers
Code:
1    <xsl:template match="c">
2        <xsl:apply-templates select="." mode="if_template" />
3    </xsl:template>

4    <xsl:template match="*[position()=last()]" mode="if_template">
5        <xsl:value-of select="@atr" />
6    </xsl:template>

7    <xsl:template match="*|/" mode="if_template" />
i think the input file is processed as follows

1)the template from line 1 is instantiated three times, for each c element.

2)each time the current node list contains all three c elements and if you test "position()=last()" on line 2, it would be "true" only the third time when the third element is the current node. But not because it's 'last child of its parent' as you state, but becase it's the last element in the current node list as xslt1.0 spec chapter 4 Expressions states - the context position comes from the position of the current node in the current node list. ???

2)APPARENTLY FALSE:say the line1 template is instantiated with the first c element as current node. apply-templates on line2 then evaluates the select and gets the first c element only - this becomes the new current node list. the current node becomes the only element in the node list.

3)APPARENTLY FALSE:template on line4 is matched, because current node list contains just one node and position()=last()=1.

from your previous post i also don't see, why should you get different result if you replace line 2 with <xsl:if test="position()=last()"> etc

thanks for your endless(i hope) patience ...

 
Old February 21st, 2007, 11:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please see section 5.3 Patterns in the XSLT 1.0 spec:

A pattern is defined to match a node if and only if there is possible context such that when the pattern is evaluated as an expression with that context, the node is a member of the resulting node-set. When a node is being matched, the possible contexts have a context node that is the node being matched or any ancestor of that node, and a context node list containing just the context node.

With the expression *[position()=last()] there is no node in the document such that this expression will select your c1 and c2 elements.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 12:07 PM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, i've read 5.2 Patterns(http://www.w3.org/TR/xslt#patterns)
i see (spec section 4) context means

1)context node = current node
2)context position = position() = position of the current node in the current node list
3)context size = last() = size of the current node list

so if i sum up this context definition, section 5.2 and your last post, i see that there is no way that current node = c1 and current node list = c1(and so position()=last() holds)?

that means <apply-templates select="c[position()=1]"> doesn't change the current node list to c1 as i understood from section 5.1 Processing Model?

i know this is fundamental, but the specification doesn't define the content of current node list very clearly for me and the books i have just don't bother explaining the specification and instead invent their own description of the processing model, preferably without the term current node list.

i hope all this still relates to the subject.

 
Old February 25th, 2007, 02:24 PM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CONCLUSIONS

xsl:if is not syntax sugar.

why the above mentioned proposal doesn't work? the test expression in xsl:if evaluates in a context with current node list containing nodes selected in apply-templates *up the stack*. the match pattern containing a predicate [...] is evaluated in a context with "siblings of the context node that match the NodeTest as the context node list". if you use the select attribute in apply-templates to select anything else than all siblings(and nothing else) or if you use xsl:sort, these two contexts have different context node lists.

if you don't use @select, the lists are identical, but you can't use variables in patterns and so you can't use it in xsl:if test expr for the proposal to work.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
syntax of xsl:if statement EstherMStrom XSLT 3 February 14th, 2005 05:47 PM
Noob needs help with XSL syntax question lancia12 XSLT 1 October 1st, 2004 08:45 AM
xsl:sort- select attribute, syntax question Flashlight XSLT 3 August 14th, 2003 12:27 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.