 |
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
|
|
|

April 17th, 2008, 07:59 AM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 17th, 2008, 08:14 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

April 17th, 2008, 08:25 AM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

April 17th, 2008, 08:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|

April 17th, 2008, 08:39 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

April 17th, 2008, 08:46 AM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Sam and Michael, that did the trick.
|
|
 |