473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Finding a set of Relative Nodes

Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am
trying to query the following document

<Root>
<Child>Name</Child>
<Child>John</Child>
<Child>Smith</Child>
<Child>23</Child>
<Child>Name</Child>
<Child>Peter</Child>
<Child>Jones</Child>
<Child>Name</Child>
<Child>David</Child>
<Child>Brown</Child>
</Root>

I can retrieve the nodeset which contains nodes with the text Name
using the following query:
theRootNode.selectNodes("*[contains(text(),'Name')]")
How can I find all nodes that come one (or two) after a node that
contains the name text? i.e. I want all the first names (nodeset -
John, Peter, David) or all the surnames.
Note positional information will not work - note John Smith has an age
node and the others do not.
Any answers? Is this actually possible using Xpath?
Jul 20 '05 #1
3 1718
Jamie Green wrote:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am
trying to query the following document

<Root>
<Child>Name</Child>
<Child>John</Child>
<Child>Smith</Child>
<Child>23</Child>
<Child>Name</Child>
<Child>Peter</Child>
<Child>Jones</Child>
<Child>Name</Child>
<Child>David</Child>
<Child>Brown</Child>
</Root>

I can retrieve the nodeset which contains nodes with the text Name
using the following query:
theRootNode.selectNodes("*[contains(text(),'Name')]")
How can I find all nodes that come one (or two) after a node that
contains the name text? i.e. I want all the first names (nodeset -
John, Peter, David) or all the surnames.
Note positional information will not work - note John Smith has an age
node and the others do not.
Any answers? Is this actually possible using Xpath?
It's non-trivial, but here's a way to pick out just the age:

Child[not(contains(text(),'Name')) and
not(contains((preceding-sibling::*)[last()],'Name')) and
not(contains((preceding-sibling::*)[last()-1],'Name')) and
contains((preceding-sibling::*)[last()-2],'Name')
]

Hideous, isn't it? :-)

Also if it helps, here's how to transform it using XSLT. There are
probably more elegant ways to do this, but I think that if you're able
to run this transform first, subsequent processing would be MUCH easier.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:output method="xml"
indent="yes" />

<xsl:template match="Root">
<people>
<xsl:apply-templates select="Child" />
</people>
</xsl:template>

<xsl:template match="Child">
<xsl:variable name="namepos">
<xsl:value-of select="position()" />
</xsl:variable>
<xsl:if test="contains(text(),'Name')">
<xsl:element name="person">
<xsl:apply-templates select="//Child[number($namepos+1)]"
mode="firstname" />
<xsl:apply-templates select="//Child[number($namepos+2)]"
mode="lastname" />
<xsl:apply-templates select="//Child[number($namepos+3)]" mode="age" />
</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template match="Child" mode="firstname">
<xsl:if test="not(contains(text(),'Name'))">
<xsl:element name="firstname">
<xsl:value-of select="." />
</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template match="Child" mode="lastname">
<xsl:if test="not(contains(text(),'Name'))">
<xsl:element name="lastname">
<xsl:value-of select="." />
</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template match="Child" mode="age">
<xsl:if test="not(contains(text(),'Name'))">
<xsl:element name="age">
<xsl:value-of select="." />
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Here is the output of the transform when performed on your sample data
above:

<people>
<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
<age>23</age>
</person>
<person>
<firstname>Peter</firstname>
<lastname>Jones</lastname>
</person>
<person>
<firstname>David</firstname>
<lastname>Brown</lastname>
</person>
</people>

Ed

Jul 20 '05 #2

"Jamie Green" <ja*********@yahoo.com> wrote in message
news:59**************************@posting.google.c om...
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am
trying to query the following document

<Root>
<Child>Name</Child>
<Child>John</Child>
<Child>Smith</Child>
<Child>23</Child>
<Child>Name</Child>
<Child>Peter</Child>
<Child>Jones</Child>
<Child>Name</Child>
<Child>David</Child>
<Child>Brown</Child>
</Root>

I can retrieve the nodeset which contains nodes with the text Name
using the following query:
theRootNode.selectNodes("*[contains(text(),'Name')]")
How can I find all nodes that come one (or two) after a node that
contains the name text?


Use:

/*/*[. = 'Name']/following-sibling::*[position() &lt; 3]

This will select (when evaluated against your source.xml) the following
nodes:

<Child>John</Child>
<Child>Smith</Child>
<Child>Peter</Child>
<Child>Jones</Child>
<Child>David</Child>
<Child>Brown</Child>
Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Jul 20 '05 #3
Thanks guys - these both make sense.

I have actually changed my approach from the one I originally
requested an answer for (due to knock on effect of handling the
resulting nodesets which would not take account of null queries). A
new problem has arisen however (don't they always).

I am now selecting a set of "Key" nodes (eg. selectnodes("*[. =
'Name']"), and then scanning from each node using the
preceding-sibling and following-sibling methods, matching on position
or relative to a node with some text value.

As part of this, I want to compare the text value of a
following-sibling node with that of the current 'key' node. So, if I
was searching from a node with the value 'John', I want to find the
next one that is called 'John'.

If I use selectnodes("following::sibling*[self::./text() = text()]") I
get all following siblings; I relaise this is due to the 'self'
reference referring to the following-sibling element. How can I refer
back to the original 'Key' node and make this comparison dynamically?

Rgds

Jamie
Jul 20 '05 #4

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

Similar topics

4
by: Porthos | last post by:
Hi All, I'm trying to find the minimum value of a set of data (see below). I want to compare the lengths of these attribute values and display the lowest one. This would be simple if I could...
2
by: mlybarger | last post by:
i'm trying to use xpath to find nodes that don't match any other nodes (of a different type). here's an example xml: <a> <b> <c>one</c> <c>three</c> <d> <e>one</e>
4
by: Ken Kast | last post by:
Here's my situation. I have a statically positioned table that has an image in a cell. I also have some layers, defined by absolute-positioned DIVs for some animation. Everything works until I...
2
by: Pallavi | last post by:
Hello, Suppose I want to highlight a piece of text. I get the x & y co-ordinates on the screen. And using dhtml I am able to highlight the text. Then I try on another machine with different...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
8
by: Paul J Lay | last post by:
I am trying to find nodes in an xml document using the XmlDocument class and having some problems. Everything seems to work OK when there are no encoded characters (< = &lt;) but when I try to...
6
by: Hans Kamp | last post by:
Is it possible to write a function like the following: string ReadURL(string URL) { .... } The purpose is that it reads the URL (determined by the parameter) and returns the string in which...
6
by: meh | last post by:
I can figure out the total number of nodes in a given tree but what I'd like to know is what is the Selected Nodes relationship to the entire tree i.e This is node n out of nnn nodes. In most of...
1
by: Elmo Watson | last post by:
forgive me, but I'm fairly new to VB.Net - having only used VB6 in the past - - I can easily place a node on the treeview (tv1.nodes.add(key, text) BUT - in VB6, we'd add a child node...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.