Accessing specific node of a node-set (XSLT and XML)

I have an xml document containing…

<section>Some Text Here</section>
<section>Some more Text here</section>

<section>Even more here</section>

Given that I am trying to obtain the contents of, say, the second node of that set, how would I do that?

I’m fairly new to XSLT … and cant for the life of me figure out how to do this. :confused:

I am new as well to XSLT though from what I’ve read, you could maybe use <xsl:for-each select=‘section’> with <xsl:number …> to try and create some kind of counter.

Once you reach 2, you output the contents using <xsl:apply-templates />.

You may need to somehow use xsl:next and xsl:otherwise within the xsl:for-each loop. Failing this, you cound have a section1, …section2, …section3, etc

Though this is proberly not acceptable is it? :rolleyes: Hope this help you out. Try having a look at and for something like this. :smiley:

The first thing I’d suggest is adding an ID attribute to the section element, that way you can uniquely identify them for selection. I recoded your example with this suggestion and came up with this:

first, the xml file:

<?xml version="1.0"?>
    <section sectionID="1">Some Text</section>
    <section sectionID="2">Some more text</section>
    <section sectionID="3">And some more Text</section>
    <section sectionID="4">Yet more Text</section>
    <section sectionID="5">Where will it end?</section>
    <section sectionID="6">Right here.</section>

then the stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">

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

    <xsl:template match="section[@sectionID = '2']">
        <xsl:value-of select="." />

    <xsl:template match="text()">

When I run this through the MSXML parser, I get the following:

<?xml version="1.0" encoding="UTF-16"?>
<selected>Some more text</selected>

A note about the stylesheet: without the third empty template with the select=“text()” attribute, you get all the nodes’ content. This template filters out the other nodes by turning off the default rule for text nodes.

(I got this from the Steve Holzner book Inside XSLT by New Riders Publishing.)

Hope it helps.


Hmm, ok, thanks for that! - It’s what I was considering, but I’d rather not HAVE to specify id’s…

This seems to work however:

<xsl:value-of select=“/sections/section[2]”/>

There’s nothing wrong with that is there?

Originally posted by GavinJ
[B]Hmm, ok, thanks for that! - It’s what I was considering, but I’d rather not HAVE to specify id’s…

This seems to work however:

<xsl:value-of select="/sections/section[2]"/>

There’s nothing wrong with that is there? [/B]

Not that I know of. I haven’t used that syntax before (not that that should necessarily mean anything :slight_smile: ), but I assume that’s picking an element out by its ordinal position in an array of elements. The only caveat I can see with it is that you would have to be sure that the element at position 2 is always what you’re expecting. If the xml gets rebuilt and there is something else in that element’s position, it could break your code.

Looking at your xml Jemlac, this looks like a good bet. I’ll keep it in mind if I come across this problem myself. :smiley:

check out some of the links in my sig, especially

Aye, Zvon is a good site for beginners. I’ve been there a few times, downloading their tutorials. Currently, I am looking over their one on XLST.

They cover SVG, the DOM, xHTML, XML, and a tonne of other langauges/technologies. Highly recommended. :stuck_out_tongue:

GavinJ, the code you posted is perfectly valid. You are specifying which child node you want.