473,769 Members | 5,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Xpath to get name with same name at different depths

Hi,
I am very new to XML and XPATH. I have made a sample XML fileto ask
my question.

<?xml version="1.0" encoding="utf-8"?>
<Test>
<A>
<B>
<C>
<D>
<NAME>Jack</NAME>
</D>
</C>
</B>
</A>
<J>
<NAME>James</NAME>
</J>
<X>
<Y>
<NAME>Tom</NAME>
</Y>
</X>
<Z>
<NAME>Peter
<W>
<NAME>Harry</NAME>
</W>
</NAME>
</Z>
</Test>
From the XML above how would I get a set of all the NAME xml nodes

using XPATH (Note that the last name node contains another name node)??
The true reson I need to do this sort of query is to get a list of
Table nodes (that can contain other Tables) as I have run some html
through SGMLReader.
Also how would I just get the lowest depth name nodes i.e. so peter is
not returned but jack,JAmes, Tom and Harry are.

Thanks for any help,
Nick

Feb 2 '06 #1
2 2435


ni*********@hot mail.com wrote:

From the XML above how would I get a set of all the NAME xml nodes using XPATH (Note that the last name node contains another name node)??


XPath (1.0) can only select existing nodes, to select all NAME element
nodes at all levels use
//NAME
Also how would I just get the lowest depth name nodes i.e. so peter is
not returned but jack,JAmes, Tom and Harry are.


The NAME elements are not leaf nodes, they have text child nodes.
If you want to select all NAME element nodes which do not have any
element child nodes you can use e.g.
//NAME[not(*)]
If you really want the leaf text nodes inside of these elements then you
could use e.g.
//NAME[not(*)]/text()

Check out an XPath tutorial, there are lots on the web, XPath 1.0 is
years old.

One example introduction is here:
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/xmlsdk/html/6da1b6e3-256e-4919-8848-53b425f72ed1.as p>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 2 '06 #2
ni*********@hot mail.com wrote:
Hi,
I am very new to XML and XPATH. I have made a sample XML fileto ask
my question.

<?xml version="1.0" encoding="utf-8"?>
<Test>
<A>
<B>
<C>
<D>
<NAME>Jack</NAME>
</D>
</C>
</B>
</A>
<J>
<NAME>James</NAME>
</J>
<X>
<Y>
<NAME>Tom</NAME>
</Y>
</X>
<Z>
<NAME>Peter
Don't do this. Mixing character data content and element content in the
same element is something you only do in normal text documents (eg HTML
paragraphs and the equivalent where you have continuous text, not in XML
data.
<W>
<NAME>Harry</NAME>
</W>
</NAME>
</Z>
</Test>
From the XML above how would I get a set of all the NAME xml nodes using XPATH (Note that the last name node contains another name node)??


Design your document so that names really are all kept in NAME elements
with no mixing.
The true reson I need to do this sort of query is to get a list of
Table nodes (that can contain other Tables) as I have run some html
through SGMLReader.
Also how would I just get the lowest depth name nodes
By how much the lowest? The single lowest one (Jack) or some collection
of the N lowest ones?
i.e. so peter is
not returned but jack,JAmes, Tom and Harry are.


If you redesign it so the last bit goes

<Z>
<NAME>Peter</NAME>
<W>
<NAME>Harry</NAME>
</W>
</Z>

then the XPath expression count(ancestor: :*) will return the depth for
each NAME node.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Feb 2 '06 #3

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

Similar topics

1
6825
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
6
2931
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
5
2535
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
6
2169
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
3
2399
by: Colin Green | last post by:
I have come across what seems like a failing in the .Net XML classes. Many people have posted requesting how to write an XPath query with namespace prefixes that works. The solution shown in all the posts I have seen is of the form: --------------------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(@"c:\foobar.xml"); XmlNamepaceManager nmMgr = newNamespaceManager(doc); nmMgr.AddNamespace("abc", "www.my.uri");
0
1727
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Loan"> <xs:complexType> <xs:sequence> <xs:element name="Borrower" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="BorrID" use="required">
5
17700
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the file as displayed when I open the xml file in VS .net 2003 IDE. When I perform the same procedure in C# the stream reader obkect displays a chaotic mess. Lots of whitespace after and "\r" and "\n" after each element. The problem is that I cannot...
8
2198
by: Jean-François Michaud | last post by:
Who in the name of #%@! thought this one out?? I noticed this behavior when trying to debug a problem I was having. I used this logical expression and some XPATH in a specific sequence of instructions that allow me to transform a CALS table model into our own specific table model and I used this expression: <xsl:if test="self::node()=../CELL and self::node()">...
14
17175
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
9583
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
10210
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
10039
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
9990
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
6668
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
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.