473,387 Members | 1,859 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.

Get XPath position of an XmlNode

Guy
Hi,

I read an XML file to an XMLDocument and iterate through its nodes. How do I
get the XPath position (index) of a certain element? For example If I on the
second "b" node I want to get "2":

<a>
<b/>
<c/>
<b/> <--- This one
</a>

Is it possible?
Nov 12 '05 #1
4 11527


Guy wrote:
I read an XML file to an XMLDocument and iterate through its nodes. How do I
get the XPath position (index) of a certain element? For example If I on the
second "b" node I want to get "2":

<a>
<b/>
<c/>
<b/> <--- This one
</a>


As long as there are no namespaces involved the following example shows
you how to count the siblings with the same name that precede the
current node:

string xmlSource = @"<a>
<b/>
<c/>
<b/>
</a>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlSource);
XmlNodeList elements = xmlDocument.GetElementsByTagName("*");
foreach (XmlNode node in elements) {
Console.WriteLine("NodeName: {0}, position: {1}.", node.LocalName,
node.SelectNodes("./preceding-sibling::" +
node.LocalName).Count + 1);
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Guy
Thanks Martin,

It seems that this is what I need, but unfortunately I do have namespaces ...
Can you instruct me how to do it?

Many thanks in advance!

"Martin Honnen" wrote:


Guy wrote:
I read an XML file to an XMLDocument and iterate through its nodes. How do I
get the XPath position (index) of a certain element? For example If I on the
second "b" node I want to get "2":

<a>
<b/>
<c/>
<b/> <--- This one
</a>


As long as there are no namespaces involved the following example shows
you how to count the siblings with the same name that precede the
current node:

string xmlSource = @"<a>
<b/>
<c/>
<b/>
</a>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlSource);
XmlNodeList elements = xmlDocument.GetElementsByTagName("*");
foreach (XmlNode node in elements) {
Console.WriteLine("NodeName: {0}, position: {1}.", node.LocalName,
node.SelectNodes("./preceding-sibling::" +
node.LocalName).Count + 1);
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


Guy wrote:

It seems that this is what I need, but unfortunately I do have namespaces ...
Can you instruct me how to do it?


With the following changes the results should be correct to find the
position in terms of elements with the same local name and the same
namespace URI:

string xmlSource = @"<a xmlns=""http://example.com/ns1"">
<b/>
<b xmlns="""" />
<pf2:b xmlns:pf2=""http://examle.com/ns2"" />
<c/>
<pf2:b xmlns:pf2=""http://examle.com/ns2"" />
<b/>
<b/>
<pf3:c xmlns:pf3=""http://example.com/ns1"" />
</a>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlSource);
XmlNodeList elements = xmlDocument.GetElementsByTagName("*");
foreach (XmlNode node in elements) {
const string prefix = "pf";
string xPathExpression;
XmlNamespaceManager xmlNamespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable);
if (node.NamespaceURI != null) {
xmlNamespaceManager.AddNamespace(prefix, node.NamespaceURI);
xPathExpression = "./preceding-sibling::" + prefix + ":" +
node.LocalName;
}
else {
xPathExpression = "./preceding-sibling::" + node.LocalName;
}

Console.WriteLine("{{{0}}}{1}, position: {2}.",
node.NamespaceURI, node.LocalName,
node.SelectNodes(xPathExpression, xmlNamespaceManager).Count + 1
);
Console.WriteLine();
}

Result here is

{http://example.com/ns1}a, position: 1.

{http://example.com/ns1}b, position: 1.

{}b, position: 1.

{http://examle.com/ns2}b, position: 1.

{http://example.com/ns1}c, position: 1.

{http://examle.com/ns2}b, position: 2.

{http://example.com/ns1}b, position: 2.

{http://example.com/ns1}b, position: 3.

{http://example.com/ns1}c, position: 2.
Give some feedback in the group whether that is what you want and it
works for you as intended.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4
Guy
Thanks Martin, It's exactly what I needed!!!

"Martin Honnen" wrote:


Guy wrote:

It seems that this is what I need, but unfortunately I do have namespaces ...
Can you instruct me how to do it?


With the following changes the results should be correct to find the
position in terms of elements with the same local name and the same
namespace URI:

string xmlSource = @"<a xmlns=""http://example.com/ns1"">
<b/>
<b xmlns="""" />
<pf2:b xmlns:pf2=""http://examle.com/ns2"" />
<c/>
<pf2:b xmlns:pf2=""http://examle.com/ns2"" />
<b/>
<b/>
<pf3:c xmlns:pf3=""http://example.com/ns1"" />
</a>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlSource);
XmlNodeList elements = xmlDocument.GetElementsByTagName("*");
foreach (XmlNode node in elements) {
const string prefix = "pf";
string xPathExpression;
XmlNamespaceManager xmlNamespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable);
if (node.NamespaceURI != null) {
xmlNamespaceManager.AddNamespace(prefix, node.NamespaceURI);
xPathExpression = "./preceding-sibling::" + prefix + ":" +
node.LocalName;
}
else {
xPathExpression = "./preceding-sibling::" + node.LocalName;
}

Console.WriteLine("{{{0}}}{1}, position: {2}.",
node.NamespaceURI, node.LocalName,
node.SelectNodes(xPathExpression, xmlNamespaceManager).Count + 1
);
Console.WriteLine();
}

Result here is

{http://example.com/ns1}a, position: 1.

{http://example.com/ns1}b, position: 1.

{}b, position: 1.

{http://examle.com/ns2}b, position: 1.

{http://example.com/ns1}c, position: 1.

{http://examle.com/ns2}b, position: 2.

{http://example.com/ns1}b, position: 2.

{http://example.com/ns1}b, position: 3.

{http://example.com/ns1}c, position: 2.
Give some feedback in the group whether that is what you want and it
works for you as intended.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #5

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

Similar topics

2
by: Hans-Michael Rupp | last post by:
Hallo! How can find out the postion of a node with a particular content in the nodeset WITHOUT being in the context? I would like have something like <xsl:variable name="position"...
1
by: Mark Snelling | last post by:
Hi, I have a question regarding XPath and namespaces. I have a piece of XML... <Cred> <Meta> <Type xmlns='syncml:metinf'>syncml:auth-basic</Type> </Meta> <Data>QnJ1Y2UyOk9oQmVoYXZl</Data>...
1
by: mfraser | last post by:
Ok I have the following XML: <Root> <DataType xmlns:vi="http://acme.com/ValueItem.xsd" xmlns:dd="http://acme.com/DataDef.xsd" DataTypeID="1" xmlns="http://acme.com/ConstantDataType.xsd"> <Data>...
3
by: akunamatata | last post by:
Hello everyone, I contact this discussiongroup because I encountered a little problem with XSL. Let me explain it: I have following file "position.xml": <?xml version="1.0"?>...
3
by: awebguynow | last post by:
with an xpath expr: step1/step2 I'm trying to get the position() of step2, using the qualifying predicate. I've seen some suggestions, of ways to do this, using count() instead, but I'm not...
1
by: jesper_lofgren | last post by:
Hello, i need to help here. I have a xmlnode as you can se below (full xml is in the bottom). I wonder how i can get the values inside the CSammanstallningLista . <CSammanstallning...
1
by: jesper_lofgren | last post by:
Hello, i need to help here. I have a xmlnode as you can se below (full xml is in the bottom). I wonder how i can get the values inside the CSammanstallningLista . <CSammanstallning Id="Omrade"...
5
by: Christof Nordiek | last post by:
When I load a XML-Document from a file, and then get an XmlNode from it, is there any way to get the its original position in the file (line, character)? I want to report it, when there is...
1
by: thomasv | last post by:
Hi, I want to convert the following XML (OpenOffice Calc file): <?xml version="1.0" encoding="UTF-8"?> <office:document-content ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.