Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slect single node from a sibling set

Status
Not open for further replies.

freeups

Technical User
Oct 23, 2006
1
US
Ameteur XMLer here. Forgive misused terminology.
I am wondering if there is an xpath expression which allows me to select a single node from a group of sibling nodes.

XML:

Code:
<whereto>
<state>colorado</state>
<state>florida</state>
<state>texas</state>
<state>utah</state>
<state>newjersey</state>
<state>newyork/state>
<state>arizona</state>
<state>hawaii</state>
</whereto>


XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
    <xsl:template match="whereto">
        <html>
            <body>
                <table border="1" cellpadding="3">
                    <th>
                        Places to go
                    </th>
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="state">
        <tr>
            <td>
                <xsl:value-of select="."/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>


As opposed to dumping all data in my <state> nodes into a table I would only like to produce the Data from the first <state> node -- Colorado. All I am currently familiar with is the <xsl:value-of> tag, which will in turn dump all data. I would like to display Colorado only. Clearly the value-of tag is an incorrect expression to produce such results. I'm just not sure what is.

Or to complicate things even more, is it possible to produce a result for the <state> node containing data Texas, skipping the two results prior to the data Texas.

Hope I am clear enough. Difficulty expressing myself over here =o
 
Yes, it is quite possible. XPath has some means to address every component of an XML document.

In the context of the whereto element, you might try
Code:
<xsl:apply-templates select="*[text()='Texas']"/>

Again in the context of the whereto element, the first state element is simply
Code:
state[1]
or more thoroughly
Code:
state[position()=1]

You can find a decent XPath tutorial here.

[small]All examples untested![/small]

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top