Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 June 7th, 2005, 12:14 PM
Registered User
 
Join Date: Jun 2005
Location: Washington, DC, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to cedar
Default Is parent first element in series

I'm glad to see experienced people here, especially Michael Kay, whose XSLT book got me started in XSLT.

I'm having a problem determining if a parent node is the first one in the series.

The XML:

<xmlfile>
  <a>
    <b>This is where I am</b>
  </a>
  <a>
    <b>Here's somebody else</b>
  </a>
  <a>
    <b>And yet another</b>
  </a>
</xmlfile>

The XSLT:
<xsl:template match = "b">
  <xsl:apply-templates />

  <xsl:if test = "parent::a[position()=1]">
    <xsl:text>- at the first a</xsl:text>
  </xsl:if>
</xsl:template>

Desired Output:
This is where I am - at the first a
Here's somebody else
And yet another

Actual Output:
This is where I am - at the first a
Here's somebody else - at the first a
And yet another - at the first a

This is actually a simplistic recreation of the actual problem, but seeing as how I'm dealing with a 10k XSLT, and complex inputs to boot, I think this slice should solve it. I think the solution could be googled, but I haven't got the terminology to find it. I've only been doing XSLT for a few weeks, so sorry if it's a newb question.


 
Old June 7th, 2005, 06:43 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The expression

parent::a[position()=1]

selects all the parents of the the context node that are called a, and then selects the first of these. Of course, a node can only have one parent, so this doesn't work.

You want

if test="not(parent::a/preceding-sibling::a)"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 8th, 2005, 10:34 AM
Registered User
 
Join Date: Jun 2005
Location: Washington, DC, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to cedar
Default

It's the simplicity of your solutions that make you the genuis you are. Thanks for the solutions, and for the books.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Value of parent element of current-grouping-key() bonekrusher XSLT 2 September 9th, 2007 06:29 AM
Can I promote an element by not copying a parent? dmorin XSLT 1 August 9th, 2006 03:52 PM
How to delete an element and parent node. crossedge XSLT 1 March 14th, 2006 05:10 AM
Accessing a element of parent window in child wind livehed BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 June 28th, 2005 10:26 AM





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