473,396 Members | 1,812 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,396 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 11528


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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.