tMan wrote:
whats the xpath to get all the empty nodes in a document. in the xml below i want to get the nodes <empty> and <t>
all other nodes either have a child node or text in them
<root>
<test>
<se>
<li>test</li>
</se>
</test>
<bs>
<empty></empty>
<re>
some other test
<t>
</t>
</re>
</bs>
</root>
You say you want to look for empty nodes but I assume you want to look
for empty elements, an XPath expression for that is
//*[not(node())]
However the <t> element is not really empty, it has white space text in
it so you need make sure that you parse in a way that such white space
is ignored. Here is an example C# program using XPathDocument and
XPathNavigator:
using System;
using System.Xml;
using System.Xml.XPat h;
public class Test20040710 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new
XPathDocument(@ "test20040710.x ml", XmlSpace.Defaul t); // Default is
important to ignore space
XPathNavigator xpathNavigator = xpathDocument.C reateNavigator( );
XPathExpression xpathExpression =
xpathNavigator. Compile("//*[not(node())]");
XPathNodeIterat or xpathIterator =
xpathNavigator. Select(xpathExp ression);
while (xpathIterator. MoveNext()) {
Console.WriteLi ne("Found element: {0}.", xpathIterator.C urrent.Name);
}
}
}
Output with your example document is
Found element: empty.
Found element: t.
But if you change one line to
XPathDocument xpathDocument = new XPathDocument(@ "test20040710.x ml",
XmlSpace.Preser ve);
then the t element is no longer empty and the result is
Found element: empty.
only.
--
Martin Honnen
http://JavaScript.FAQTs.com/