all groups > dotnet xml > april 2004 >
You're in the

dotnet xml

group:

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


Locating xmlns nodes in XML documents (for XML document metrics) shawnk
4/28/2004 1:01:07 PM
dotnet xml: 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 MoveToFirstNamespace() an
MoveToNextNamespace(). 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
Re: Locating xmlns nodes in XML documents (for XML document metrics) Oleg Tkachenko [MVP]
4/29/2004 12:52:16 PM
[quoted text, click to view]

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]
Re: Locating xmlns nodes in XML documents (for XML document metrics) shawnk
4/29/2004 7:56:02 PM
Oleg

Thank you soooo much :-

The code below works

public stati
bool Select_namespace_references(XPathNavigator p_xml_cur

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

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

XPathNavigator l_xml_cur = null
XPathNodeIterator 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.Select(l_XPath_query_srh_str)


catch (ArgumentException l_XPath_query_srh_exp

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

Console.WriteLine( l_XPath_query_srh_exp.Message ); // Print the error message
Console.WriteLine( l_XPath_query_srh_exp.Source ); // Name of application or object that caused the error
Console.WriteLine( l_XPath_query_srh_exp.StackTrace ); // String that contains the stack trace for this exception

return false


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

Console.WriteLine(l_xml_nod_itr.Current.Name)

while (l_xml_nod_itr.MoveNext()

Console.WriteLine(l_xml_nod_itr.Current.Name)


//#---5---- Return -----------------------------------------------------------------------:_:---

return true


AddThis Social Bookmark Button