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 August 29th, 2004, 10:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default Understanding context node in templates

Could someone please help me understanding the TEMPLATE MATCH. This puzzles me:

xml:

<r>
  <order>
    <item>First Item</item>
  </order>
  <neworder>
    <item>Second Item</item>
  </neworder>
</r>

-----------

Using this template:

<xsl:template match="/r/neworder">
  <xsl:value-of select="item"/>
</xsl:template>

..gives me both the order and neworder values, whereas this one:

<xsl:template match="/">
  <xsl:value-of select="r/neworder/item"/>
</xsl:template>

..gives me what I expected.

Setting the context node to r/neworder, I would expect the template to disregard the order node.

My Wrox book states: "..that whatever the template uses for its match attribute becomes the context node for that template. This means that all XPath expressions within the template are relative to that node."

So, where's my understanding failing?
 
Old August 30th, 2004, 10:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Your first template is not matching the other items. The default templates are being invoked and leading to the results you see. To prove this try adding an extra line and you'll see the position is only next to neworder/items:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/r/neworder">
    <xsl:value-of select="position()"/><xsl:value-of select="item"/>
  </xsl:template>
</xsl:stylesheet>
For an explanation of default template see Michael Kay's Wrox books on XSLT or follow http://www.dpawson.co.uk/xsl/sect2/defaultrule.html



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old August 30th, 2004, 12:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe! - and thanks. I understand the concept of default templates, or rather why they're invoked. It's just that I can't get into my head, what the difference is in the two match and select examples I wrote. Somehow they seem to be equal to me, only split up in two different ways. I guess thats what's bothering me a lot.

If I start off with a <xsl:template match="/"> and an <apply-template select="/r/neworder"/> I've no problem with the following <xsl:template match="r/neworder"> and then the .. select="item"> part.

It looks as if I can only invoke a 'template match' using an apply template statement. And I don't think that's the case!? I hope I'm making my self clear.

 
Old August 31st, 2004, 04:21 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well in the first example the default templates cause the document root to be matched and processed which recursively calls apply-templates on all children and outputs text as it goes along. You can add the empty template
Code:
<xsl:template match="*"/>
if you want to supress unawanted output.
Another good way if to step through the transform in an XSLT debugger if you have one, XMLSpy has one, I think, as does VS.Net 2005.




--

Joe (Co-author Beginning XML, 3rd edition)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to set context in web.xml or context.xml. dchicks Apache Tomcat 1 March 7th, 2008 07:59 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM
Generic templates and only one node() Clyne XSLT 4 January 5th, 2007 12:47 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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