[Home] [By Thread] [By Date] [Recent Entries]

Subject: finding position in list of siblings
From: "John C Cartwright" <John.C.Cartwright@xxxxxxxx>
Date: Fri, 09 Jul 2004 12:56:57 -0600
Hello All,

I'm trying to find the context node's position relative to a list of identically named nodes. For example, given the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<FAMILY>
  <SISTER/>
  <SISTER/>
  <BROTHER/>
  <BROTHER/>
  <BROTHER/>
</FAMILY>

I would like the 3 BROTHER nodes to show their position as 1,2,3 respectively rather than 3,4,5.

I've tried the following template, but can't seem to get the constraint right to eliminate from the node list all but the <BROTHER> elements. Can someone show me the way to do this?


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>


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

<xsl:template match="BROTHER">
   <xsl:value-of select="count(BROTHER/preceding-sibling::*)" />
   <xsl:value-of select="count(preceding-sibling::node()[.='BROTHER'])" />

</xsl:template>
</xsl:stylesheet>


Thanks!




-- john

=====================================================
John Cartwright
Associate Scientist
Geospatial Data Services Group
CIRES, National Geophysical Data Center/NOAA
(303) 497-6284
John.C.Cartwright@xxxxxxxx
=====================================================

Current Thread
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member