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 April 17th, 2008, 07:59 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding the first child element

Ok, so I've been having loads of problems trying to find the first child element in a given XML tree. Here is the example XML:

<page-components>
  <courses-component />
  <courses-component />
  <courses-component />
  <courses-component />
</page-components>

And here is the associated XSLT (that does not work):

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

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

<xsl:template match="courses-component">
  <xsl:if test="position()=1">
    ..(do some outputting for the first element here)...
  </xsl:if>

  ...(rest of my XSL template here)...

</xsl:template>

</xsl:stylesheet>

Now here's the catch, I cannot use two different <xsl:templates> with one XPATH being "/page-components/courses-component" and the other being "/page-components/courses-components[1]" (unless I absolutely must, but I don't like coding twice if I can avoid it). Also, I don't have the ability to change the <xsl:apply-templates>, so I cannot do <xsl:apply-templates select="/page-components/courses-components[1]">. Does anyone know of another solution?

Thank you,
Henry
 
Old April 17th, 2008, 08:14 AM
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

Rather than saying you have lots of problems, it's useful to say what your problems are.

Unless you're using a processor such as Microsoft or Altova which removes the whitespace text nodes, the first courses-component element is the second node, so it satisfies position()=2. I would change <xsl:apply-templates/> to <xsl:apply-templates select="*"/> to fix this. You say you can't do that, but you haven't said why.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 17th, 2008, 08:25 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,
Thank you for the quick response. I apologize for being so unclear. I don't have access to the <xsl:apply-templates /> because that is located in another file that I do not have edit rights to. I only have edit rights for the <courses-component /> element file that is being imported by the another file (the root file).

Also, I forgot to mention, that there can be one or more <courses-component /> as well as any other elements, such as <foo-component />, and there is no set position for any of these components. <courses-component /> can be first or <foo-component /> can be first, therefore checking position()=2 will not work.

 
Old April 17th, 2008, 08:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well that kinda changes the question a bit doesn't it.

Try:

<xsl:if test="not(preceding-sibling::courses-component)">

</xsl:if>

or something like that. Obviously this will take longer the more course-component elements you have.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 17th, 2008, 08:39 AM
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

Then you could try

<xsl:if test="not(preceding-sibling::courses-component)">

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 17th, 2008, 08:46 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Sam and Michael, that did the trick.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Attribute to Child element Conversion kudzuconf XSLT 6 July 13th, 2007 04:02 AM
child element kgoldvas XML 4 April 27th, 2006 01:51 AM
Finding the last element jeremycod XSLT 4 March 12th, 2006 05:45 PM
Finding child controls Hunter ASP.NET 1.0 and 1.1 Professional 4 October 27th, 2005 09:09 AM
XSL select by child element value vivhost XSLT 3 May 21st, 2005 02:58 AM





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