473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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/".

Jul 18 '06 #1
3 2225
Positional predicate:
<xsl:first select="child[1]">
Jul 18 '06 #2
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="@hairco lor" />
<xsl:value-of select="@eyecol or" />
</xsl:for-each>

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

Jul 18 '06 #3
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.
Jul 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6013
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on paragraphs. So far, so good: some of the paragraphs begin with a link, and there are styles in the stylesheet for link, hover etc. The first letter is still displayed as styled for the paragraph's first-letter.
6
16604
by: Veerle | last post by:
Hi, Somewhere in my html page, I have the following code: <div id="main-body"> <h2>Header text</h2> <p>Some other text ...</p> ..... <h2>Another header text</h2> <p>Some more other text ...</p>
5
1983
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() { var objectName = "ParentObject";
2
1261
by: Diane Selby | last post by:
Hi- I am confused by C#'s scoping of variables within a child scope (for loop, conditional, block of code), and was hoping someone could explain. Here's the offending code: for (int i=0; i<10; i++) { ///// }
4
3504
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i / (list.Count) ); } int i = 23;
9
1925
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13, in ? A() File "x1x2.py", line 11, in A
13
14538
by: howa | last post by:
e.g. <div class="main"> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p>
2
6387
by: ofuuzo1 | last post by:
Hei, I have the following xml file and I have tried to write xslt to extract only the values of the first "record" node. It does not work. I need some help. I used ---- to represent indent. Xml: <?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd"> ---<responseDate>2008-02-19T12:54:06Z</responseDate> ---<request xmlns="" verb="ListRecords" ......o.no</request>
2
4858
by: Claudia Fong | last post by:
Hi, I have a xml document which is like this: <profile document> <Child name = profile1 id = 1> </Child> </profile document>
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.