Re: [xsl] using step or following-sibling?

Subject: Re: [xsl] using step or following-sibling?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 03 Dec 2004 17:17:41 -0500
Matt,

At 04:00 PM 12/3/2004, you wrote:
Let's say I have the following XML:


<top> <results> <row> <emp_nm>Bill Smith</emp_nm> </row> <row> <emp_nm>Sue Johnson</emp_nm> </row> <row> <emp_nm>Matt Keller</emp_nm> </row> </results> </top>

Okay.


And in my xsl I have a template like this:

<xsl:template match="/top/results/row">
<p>I like <xsl:value-of select="emp_nm"/> and I also like his or her follower, MY TROUBLE ZONE</p>
</xsl:template>


------------------

How in the world do I replace MY TROUBLE ZONE with the value of "emp_nm" in the following row? Not all following roles, just the very next row. And how can I do it w/out using position()?

I assume you're not asking how to substitute the string. (If you are, replacing the string "MY TROUBLE ZONE" with any value at all will require some string-substitution magic, which you can find documented in the FAQ. XSLT does not excel at string manipulation but it's not impossible to do either. If you could arrange for this placeholder to be an element or a processing instruction instead of a string, your processing would be both easier and more robust.)


As to the value you want to substitute, if your context node is the row, the emp_nm of the very next row is expressed in XPath as "following-sibling::row[1]/emp_nm. While this does use position() (the predicate expands to [position()=1]), in XSLT were you to say

<xsl:value-of select="following-sibling::row/emp_nm"/>

you would get the value only of the next one, since the value-of instruction returns a string, and the rule for converting a node-set into a string is to take the string value of the first node in the node-set (which happens to be the one you want).

If there is no such following sibling, of course, nothing will get substituted.

I hope this helps,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread