Re: [xsl] XSLT: Xpath: sets: position()

Subject: Re: [xsl] XSLT: Xpath: sets: position()
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 27 Feb 2001 17:30:14 +0000
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