Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - scoping to the first child element

 
Thread Tools Search this Thread
Old 07-18-2006, 09:47 PM   #1
Default scoping to the first child element


Given this XML

<parent>
<child name="billy">
<child name="sue">
</parent>

I can use this to process each child...

<xsl:for-each select="child">
<xsl:value-of select="@name" />
</xsl:for-each>

but if I want to only output info from the first child, I need to use
this...

<xsl:value-of select="child/@name" />

Is there some way to scope to the first child like this...

<xsl:first select="child">
<xsl:value-of select="@name" />
</xsl:first>

obviously, this is a contrived example but imagine if child had 40
attributes...

<xsl:value-of select="child/@name" />
<xsl:value-of select="child/@age" />
<xsl:value-of select="child/@haircolor" />
<xsl:value-of select="child/@eyecolor" />
etc...

It seems like there should be a way to avoid having to prefix the XPath
for each select with "child/".



William Krick
  Reply With Quote
Old 07-18-2006, 09:58 PM   #2
Joe Kesselman
 
Posts: n/a
Default Re: scoping to the first child element

Positional predicate:
<xsl:first select="child[1]">
  Reply With Quote
Old 07-18-2006, 10:12 PM   #3
William Krick
 
Posts: n/a
Default Re: scoping to the first child element

Joe Kesselman wrote:
> Positional predicate:
> <xsl:first select="child[1]">



there is no xsl:first tag. I made that up to illustrate what I need.


though, I took your idea and came up with this...

<xsl:for-each select="child[1]">
<xsl:value-of select="@name" />
<xsl:value-of select="@age" />
<xsl:value-of select="@haircolor" />
<xsl:value-of select="@eyecolor" />
</xsl:for-each>

.... and it works but it seems kinda hacky to use a loop as there will
only ever be one child[1]

  Reply With Quote
Old 07-18-2006, 11:26 PM   #4
Joe Kesselman
 
Posts: n/a
Default Re: scoping to the first child element

William Krick wrote:
> ... and it works but it seems kinda hacky to use a loop as there will
> only ever be one child[1]


A good processor will recognize this usage pattern and not bother
examinining anything after the first, so it's a perfectly reasonable way
to express the problem.

An alternative that doesn't look like a loop would be to assign the
results of the child[1] search to a variable, then do the other
expressions relative to that...
<xsl:variable name="foo" select="child[1]"/>
<xsl:value-of select="$foo/@name">
and so on.

Or use a named template, applying it to select="child[1]". search.

There are probably other equivalent solutions.

XSLT is a programming language. There are often multiple ways to express
an operation. Pick the one that matches what you're trying to express.
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump