RE: [xsl] XPath question

Subject: RE: [xsl] XPath question
From: "Ross Ken" <Ken.Ross@xxxxxxxxxxxxxx>
Date: Tue, 4 Feb 2003 08:55:25 +1000
Why would you want to count them, simply recursively process the nodes with xsl:apply-templates using a mode if you have to.

eg (untested code)

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

<xsl:template match="item">
<!-- the leaf node -->
	<xsl:param name="parent-route"/>
	<xsl:value-of select="concat($parent-route,string(position()))"/>
</xsl:template>

<xsl:template match="item[item]">
<!-- not a leaf node -->
	<xsl:param name="parent-route"/>
	<xsl:apply-templates select="item">
		<xsl:with-param name="parent-route" select="concat($parent-route,string(position()),'.')"/>
	</xsl:apply-templates>
</xsl:template>

HTH,

Ken Ross
Ph: +61 7 32359370
Mob: +61 (0)419 772299
Email: Ken.Ross@xxxxxxxxxxxxxx


-----Original Message-----
From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx]
Sent: Tuesday, 4 February 2003 8:22 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] XPath question


Every time I think I have a good grasp on XPath, it slips between my fingers. Consider the following XML:

<menu>
  <item title="Vegetables">
    <item title="tubers">
      <item title="potatoes">
        <item>Idaho</item>
        <item>Maine</item>
        <item>Russet</item>
        <item>Yam</item>
      </item>
    </item>
  </item>
  <item title="Fruits">
    <item title="Apples>
      <item>Fuji</item>
      <item>Granny Smith</item>
      <item>Rome</item>
  </item>
  <item>Milk</item>
</menu>

I have an HTML application for which I must flatten out the hierachy of this XML document, yet still maintain a link to the parent element for each child element. To this end I have decided to number each element according to its position in the hierarachy and use a "dot-delimitted" format for each element's id (i.01.03.11). I have struggled in vain today to come up with an XPath expression which would tell me how many elements are on the preceding-sibling axis of the parent of a given node. Can anyone straighten me out on this?

Thanks.
-- 
Charles Knell
cknell@xxxxxxxxxx - email

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


IMPORTANT: This email (including any attachments) may contain confidential,
private or legally privileged information and may be protected by copyright. You may
only use it if you are the person(s) it was intended to be sent to and if you use it in an
authorised way. No one is allowed to use, review, alter, transmit, disclose, distribute,
print or copy this e-mail without appropriate authority.
 
If this e-mail was not intended for you and was sent to you by mistake, please
telephone or e-mail me immediately, destroy any hard copies of this e-mail and delete
it and any copies of it from your computer system. Any legal privilege and 
confidentiality attached to this e-mail is not waived or destroyed by that mistake.
 
It is your responsibility to ensure that this e-mail does not contain and is not affected
by computer viruses, defects or interference by third parties or replication problems
(including incompatibility with your computer system).




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


Current Thread