XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
K JSubject: Traversing XML Tree - Want to get next Element
Author: K J
Date: 08 Nov 2006 04:46 AM
Hi all,

I have some XSL that goes down the structure and when I get to a particular tag I want the variable Im declaring to hold just the next tag name. This is shown below:

<a>
<b>
<c/>
</b>
</a>

So what I want is to go down to <b> and then get the value of <c>. This could be different everytime so next time it could be <d/>.

Any suggestions would be appreciated,

Postnext
Tony LavinioSubject: Traversing XML Tree - Want to get next Element
Author: Tony Lavinio
Date: 08 Nov 2006 09:15 AM
It's not really clear what you are asking for a real document.
When you say 'tag' do you mean 'element'?
It is best when thinking of XML not to think of a stream of tags,
but of the elements that comprise the start and end tags and their
content.

Assuming by 'next' you mean 'first child', for the following sample
file, this would get the name of the 'first child' of each second-level
element as the value of that element in the output.

Input:
<a><b><c/></b><b><d/></b></a>

XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<a>
<xsl:apply-templates select="a/*"/>
</a>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:value-of select="local-name(*[1])"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Output:
<?xml version='1.0' ?>
<a><b>c</b><b>d</b></a>

Postnext
K JSubject: Traversing XML Tree - Want to get next Element
Author: K J
Date: 08 Nov 2006 10:45 AM
Apologies my question was prob confusing,

What I need to do is traverse down the tree and then at a specific point get the name of the next element...e.g:

<name>
<surname>
<Smith>
</Smith>
</surname>
</name>

I need to get the element name the item under <surname>, this could be <Fred> next time, I just need to be able to get the name of the element,

Thanks again,

Posttop
Tony LavinioSubject: Traversing XML Tree - Want to get next Element
Author: Tony Lavinio
Date: 08 Nov 2006 01:58 PM
local-name(*[1]) will return the name of the first child element
of the current element.

The supplied example in the previous answer should help you.

If you want the next peer instead of the next child, you should
investigate the following-sibling:: axis.

See http://www.stylusstudio.com/w3c/xpath/axes.htm or
http://www.w3.org/TR/xpath#axes

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.