473,770 Members | 5,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Locating xmlns nodes in XML documents (for XML document metrics)

I've been writing an XML document analyzer that reads XML document
and tracks the metrics of the document. Statistics such as a node count fo
each type of XML node are printed out

I am trying to locate 'xmlns' nodes using the XPath (not DOM or SAX) API
Unfortunately the XPathNavigator concept of namespace nodes for eac
element is only accessible via MoveToFirstName space() an
MoveToNextNames pace(). The XPath model constructs a namespace node lis
for EACH ELEMENT where each namespace node represents a namespace CURRENTL
IN SCOPE for the current XML node

I would like to easily locate the 'xmlns' references in an XM
document just to count them and optionally print them out. It woul
also be nice to track some namespace node statistics such as defaul
vs prefix namespace declarations

Does anyone have an idea on how to do this

Nov 12 '05 #1
2 1558
shawnk wrote:
I would like to easily locate the 'xmlns' references in an XML
document just to count them and optionally print them out. It would
also be nice to track some namespace node statistics such as default
vs prefix namespace declarations.


There is no easy way to detect namespace declarations using XPath,
because they are out of XPath data model. You can use the following
trick - select all namespace nodes on element node, which are first such
in document order (absent on element's parent):

//namespace::*[not(.=../../namespace::*)]

PS. And don't forget about implicit xml namespace, which always exists.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
Oleg

Thank you soooo much :-

The code below works

public stati
bool Select_namespac e_references(XP athNavigator p_xml_cur

//#---1---- Declare and set local state space --------------------------------------------:_:---

string l_XPath_query_s rh_str = "//namespace::*[not(.=../../namespace::*)]"

XPathNavigator l_xml_cur = null
XPathNodeIterat or l_xml_nod_itr = null; // XML Node iterato

//#---2---- Clone cursor -----------------------------------------------------------------:_:---

l_xml_cur = p_xml_cur.Clone ()

//#---3---- Set up an XML Node iterator using an XPath query -----------------------------:_:---

try
l_xml_nod_itr = l_xml_cur.Selec t(l_XPath_query _srh_str)
catch (ArgumentExcept ion l_XPath_query_s rh_exp

Console.WriteLi ne(" XPath search expression is bad!")
Console.WriteLi ne(" The following error occurred:")

Console.WriteLi ne( l_XPath_query_s rh_exp.Message ); // Print the error message
Console.WriteLi ne( l_XPath_query_s rh_exp.Source ); // Name of application or object that caused the error
Console.WriteLi ne( l_XPath_query_s rh_exp.StackTra ce ); // String that contains the stack trace for this exception

return false
//#---4---- Walk over XML namespace node list --------------------------------------------:_:---

Console.WriteLi ne(l_xml_nod_it r.Current.Name)

while (l_xml_nod_itr. MoveNext()

Console.WriteLi ne(l_xml_nod_it r.Current.Name)
//#---5---- Return -----------------------------------------------------------------------:_:---

return true

Nov 12 '05 #3

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

Similar topics

1
2169
by: mr_burns | last post by:
Hi there, I dont normally use DOM XML but have been given the task of integrating it with a web site using PHP. I have to use DOM XML as this is what is provided by the server. Basically what I am tryin to do is get it to load an XML document (that I can do) and find a specific node by its id attribute. The code I am up to is:
1
4138
by: Chris Bedford | last post by:
Hi: this question has been driving me crazy. would be grateful for any help. I am writing a version of the identity transformation to filter some documents (they happen to be wsdl docs). My problem is that for just about every element in the output document all of the namespace defintions defined in my top
4
8119
by: cyclops | last post by:
I'm trying to do XML + XSLT -> Another XML. The source XML contains multiple namespaces and XSLT will handle all possible tags under each name space. ----source---- <document xmlns="..." xmlns:a="..." xmlns:b=""> .... ----XSLT---- ....
8
1546
by: Xamle Eng | last post by:
One of the things I find most unnatural about most XML APIs is that they try to abstract both elements and text into some kind of "node" object when they have virtually nothing in common. The reason these APIs do it is to make it possible for both text and elements to be children of elements. But there is another way. The XPath/XQuery data model does not allow two consecutive text nodes. As far as I can tell, most XML processing...
0
1399
by: Philipp Schumann | last post by:
Hi there, don't know whether anyone ran into this issue formerly (in .NET 1.0). I have an XML document with a default namespace (indicating the target namespace of the XML Schema used to validate the document): <ObjectIndex xmlns="http://ref.devlink.net/opal/runtime/ObjectBaseSchema
11
4333
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
6
8393
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete event using: Dim mNewDoc As mshtml.IHTMLDocument3 mNewDoc = NewDoc(axWebBrowser1.Document) Private Function NewDoc(ByVal mInputDoc As mshtml.IHTMLDocument3) As mshtml.IHTMLDocument3
3
7276
by: Keith Patrick | last post by:
I'm doing some document merging where I want to bring in an XmlDocument and import its document element into another document deeper in its tree. However, when serializing my underlying objects, .Net likes to add these namespaces: <RootNode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ChildNode xmlns="MyObjectHierarchyNamespace/> </RootNode> The problem this is causing me is that...
2
1962
by: SMH | last post by:
I am in the process of converting all HTML documents, including many dynamic/interactive documents, to XHTML documents (because I want to incorporate SVG and MathML, among other things). I am having a problem converting document.write() statements which are not allowed in XHTML documents to statements which create the DOM nodes/elements dynamically within script elements.
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
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
10058
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...
0
9870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
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();...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
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.