[
Lists Home |
Date Index |
Thread Index
]
On 19 Nov 2002 at 16:53, Nischal Muthana wrote:
> I have an XSL file say NameList.xsl which picks
> transforms an XML file say Names.xml.
>
> The Names.xml is like,
>
> <Name>
> <Names>aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii
> jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt
> uuuu vvvv wwww xxxx yyyy zzzz</FirstNames>
> </Name>
This non-well-formed XML isn't your real code, is it?
(Names != FirstNames)
> My Xsl file has to pick up the Names node and do a
> substring and pick each value in the <Names> tag. Each
> value is of 4 characters and each value is seperated
> by a space.
>
> I am tryin to write a <xsl:foreach
> select='Name/Names'>
> and then do a substring on the element value. Can
> someone guide me through how to go about with this.
Generally the best way to handle problems like this is a functional
programming technique known as tail-recursion. The basic idea is that
your function does some operation on the first item in the input and
then calls itself with the remaining (unprocessed) input as an
argument.
Let's say you want each of those four-character names to end up as an
HTML list item. Here's a pseudo-code example of how to do that with
tail-recursion.
function namesToListItems(names) {
# assume splitString() is a function that returns a pair
# of the substring before the first space and the substring
# after the first space
firstName = firstOf(splitString(names));
remainingNames = restOf(splitString(names));
result = makeListItem(firstName) + \
namesToListItems(remainingNames);
}
And here's how it looks in XSLT:
<xsl:template name="namesToListItems">
<xsl:param name="names"/>
<LI>
<xsl:value-of select="substring-before($names,' ')"/>
</LI>
<xsl:call-template name="namesToListItems">
<xsl:with-param name="names"
select="substring-after($names,' ')"/>
</xsl:call-template>
</xsl:template>
> <xsl:value-of select="substring(Name/Names,0,4)"/>
> <xsl:value-of select="substring(Name/Names,4,4)"/>
Well, you can do that. But of course the problem is that you have to
know exactly how many names there are in every instance.
By the way, if you find yourself having a lot of XSLT questions, XSL-
List is the best place to ask. Visit www.mulberrytech.com for more
info.
Hope this helps.
--
Matt Gushee
Englewood, CO USA
|