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 May 3rd, 2005, 06:51 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jdiderik
Default return tree untill attribute is true

I am trying to create an xsl sheet that returns part of a tree from xml.
I need all page nodes with stop=0 up untill the node with attribute stop=1

stripped xml sample:

<content>
  <page id='page1' stop='0'>
    <page id='page2' stop='0'/>
    <page id='page3' stop='0'>
      <page id='page4' stop='0'/>
    </page>
  </page>
  <page id='page5' stop='0'>
    <page id='page6' stop='1'/>
    <page id='page7' stop='0'>
      <page id='page8' stop='1'/>
    </page>
  </page>
</content>

In this case I would like to have returned all pages from 1 to 6 (where it is the first in the tree to have the attribute "stop=1"
It could also be that page2 has a "stop=1" or any other page.

Any thoughts on what the xsl should look like?

Regards,
J. Diderik


 
Old May 3rd, 2005, 07:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The simple answer is like this:

<xsl:template match="page" priority="2">
  -- process the page
</xsl:template>

<xsl:template match="page[@stop=1] | page[following::page/@stop=1]"
       priority="3">
  // do nothing
</xsl:template>

However, this could be rather expensive, so whether it's viable depends on the size of your source. A faster solution, but one that's a bit harder to code, is to program the tree walking yourself (so a template for a page does apply-templates first to its first child, then to its following sibling, passing as a parameter a boolean that indicates whether stop=1 has yet been encountered.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 3rd, 2005, 08:44 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jdiderik
Default

Thank you Michael,

I must first thank you for your book "XSLT Programmer's Reference and XPath 2.0 Programmer's Reference". It's been a great help in my struggles with xml.

I seem to be unable to get the simple solution to work, but will work on that. The courses will be quite large, with possibly over 100 pages, so walking the tree would be my best option, but I will have to dive into xsl all the way before I will get the full force of xsl to work :-)

Any steps in the right direction would be of great help.

YT,
J. Diderik

 
Old May 3rd, 2005, 09:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It occurs to me there's a way of doing it that's both simple and performant in XSLT 2.0.

First set a global variable to the first page with @stop=1

<xsl:variable name="stopper" select="(//page[@stop='1'])[1]"/>

Then in your template rules distinguish whether nodes are before or after the stopper:

<xsl:template match="page[. << $stopper]">

<xsl:template match="page[. is $stopper or . >> $stopper]"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 3rd, 2005, 09:42 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jdiderik
Default

Ah, Yes....all things fall into place :-)

I started to go down that road but didn't quite get it, your help is much appriciated!

Thank you very much.

Regards,
Jeroen Diderik






Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
How to disable subform untill values selected in m method Access 3 June 29th, 2005 07:22 AM
SmartNavigation="true" CraigJones ASP.NET 1.0 and 1.1 Basics 2 September 3rd, 2004 01:29 PM
True DBGrid 8.0 KalluMama Pro VB 6 1 August 10th, 2004 01:39 AM





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