[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT: Xpath: sets: position()

Subject: Re: XSLT: Xpath: sets: position()
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 27 Feb 2001 17:30:14 +0000
xslt xpath firstchild
Hi Lee,

> Trying to get the first occurrence of an XML element marked. Can't
> be so difficult, can it? Please - what am I doing wrong?

I bet that you haven't got something like:

  <xsl:strip-space elements="*" />

in your stylesheet?

That means that when you apply templates to the children of the SPC
element, *all* its children (including comments, processing
instructions and, most importantly, text nodes) will have templates
applied to them.

The position() function gives you the position of the context node in
the context node list.  In a template, the context node is whatever
node is matched by the template (e.g. an element child of SPC in your
example) and the context node list is the list of nodes that are
currently having templates applied to them (e.g. all the children of
SPC in your example).

You probably have:

  <SPC>
    <foo />
    ...
  </SPC>

in your document, in which case the first child of the SPC element is
a text node (containing a new line and a tab or something).  That
means that no element child of SPC has a position() equal to 1.

To fix it, either apply templates only to the element children of the
SPC element:

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

or identify the element that's the first element child in another way,
such as by seeing whether it has any preceding element siblings:

   <xsl:choose>
      <xsl:when test="not(preceding-sibling::*)">
         <H3><xsl:value-of select="@TITLE"/></H3>
      </xsl:when>
      <xsl:otherwise>
         <H4><xsl:value-of select="@TITLE"/></H4>
      </xsl:otherwise>
   </xsl:choose>

or ignore all that whitespace by stripping it from the source node
tree with:

  <xsl:strip-space elements="*" />

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.