[Mono-list] XPath, XmlNode.SelectNodes and Namespaces
Jerome Laban
jlaban@wanadoo.fr
Sat, 8 Feb 2003 20:14:31 +0100
This is a multi-part message in MIME format.
------=_NextPart_000_01D0_01C2CFAE.B06DF310
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_01D1_01C2CFAE.B06DF310"
------=_NextPart_001_01D1_01C2CFAE.B06DF310
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi all,=20
As namespaces handling in XPathNavigator does not seem to be =
complete, i managed to add a rudimentary support for it.
It may not be complete, but it still allows the SelectNodes method =
to correctly return nodes in a namespace.
I also added a test in the test suite.
Can someone test it and commit if ok ?
Thanks,
J=E9r=F4me.
------=_NextPart_001_01D1_01C2CFAE.B06DF310
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1126" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi all, </FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2> As n</FONT><FONT =
size=3D2>amespaces=20
handling in XPathNavigator does not seem to be complete, i managed to =
add a=20
rudimentary support for it.</FONT></DIV>
<DIV><FONT size=3D2> It may not be complete, but it =
still allows=20
the SelectNodes method to correctly return nodes in a =
namespace.</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2> I also added a test in the test=20
suite.</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2> Can someone test it and commit if =
ok=20
?</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>Thanks,</FONT></DIV>
<DIV><FONT size=3D2>J=E9r=F4me.</FONT></DIV></BODY></HTML>
------=_NextPart_001_01D1_01C2CFAE.B06DF310--
------=_NextPart_000_01D0_01C2CFAE.B06DF310
Content-Type: application/octet-stream;
name="xml.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="xml.diff"
Index: mcs/class/System.XML/System.Xml.XPath/Expression.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /mono/mcs/class/System.XML/System.Xml.XPath/Expression.cs,v=0A=
retrieving revision 1.8=0A=
diff -u -p -r1.8 Expression.cs=0A=
--- mcs/class/System.XML/System.Xml.XPath/Expression.cs 21 Sep 2002 =
18:26:49 -0000 1.8=0A=
+++ mcs/class/System.XML/System.Xml.XPath/Expression.cs 8 Feb 2003 =
19:12:06 -0000=0A=
@@ -21,7 +21,7 @@ namespace System.Xml.XPath=0A=
internal class CompiledExpression : XPathExpression=0A=
#endif=0A=
{=0A=
- protected XmlNamespaceManager _nsm;=0A=
+ protected XmlNamespaceManager _nsm =3D new XmlNamespaceManager(new =
NameTable());=0A=
protected Expression _expr;=0A=
=0A=
public CompiledExpression (Expression expr)=0A=
@@ -44,6 +44,8 @@ namespace System.Xml.XPath=0A=
_nsm =3D nsManager;=0A=
}=0A=
public override String Expression { get { return _expr.ToString (); }}=0A=
+ public XmlNamespaceManager NsManager { get { return _nsm; }}=0A=
+=0A=
public override XPathResultType ReturnType { get { return =
_expr.ReturnType; }}=0A=
[MonoTODO]=0A=
public override void AddSort (Object obj, IComparer cmp)=0A=
Index: mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /mono/mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs,v=0A=
retrieving revision 1.11=0A=
diff -u -p -r1.11 XPathNavigator.cs=0A=
--- mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs 21 Sep 2002 =
18:26:49 -0000 1.11=0A=
+++ mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs 8 Feb 2003 =
19:12:06 -0000=0A=
@@ -9,6 +9,7 @@=0A=
=0A=
using System;=0A=
using Mono.Xml.XPath;=0A=
+using System.Xml.Xsl;=0A=
=0A=
namespace System.Xml.XPath=0A=
{=0A=
@@ -184,8 +185,16 @@ namespace System.Xml.XPath=0A=
=0A=
public virtual XPathNodeIterator Select (XPathExpression expr)=0A=
{=0A=
- BaseIterator iter =3D new NullIterator (this, new DefaultContext ());=0A=
- return ((CompiledExpression) expr).EvaluateNodeSet (iter);=0A=
+ CompiledExpression localExpr =3D expr as CompiledExpression;=0A=
+=0A=
+ XsltContext context =3D new DefaultContext ();=0A=
+=0A=
+ foreach(string ns in localExpr.NsManager)=0A=
+ context.AddNamespace(ns, localExpr.NsManager.LookupNamespace (ns));=0A=
+=0A=
+ BaseIterator iter =3D new NullIterator (this, context);=0A=
+=0A=
+ return localExpr.EvaluateNodeSet (iter);=0A=
}=0A=
=0A=
public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, =
bool matchSelf)=0A=
Index: mcs/class/System.XML/Test/SelectNodesTests.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /mono/mcs/class/System.XML/Test/SelectNodesTests.cs,v=0A=
retrieving revision 1.3=0A=
diff -u -p -r1.3 SelectNodesTests.cs=0A=
--- mcs/class/System.XML/Test/SelectNodesTests.cs 1 Jan 2003 17:37:42 =
-0000 1.3=0A=
+++ mcs/class/System.XML/Test/SelectNodesTests.cs 8 Feb 2003 19:12:07 =
-0000=0A=
@@ -223,5 +223,15 @@ namespace MonoTests.System.Xml=0A=
AssertEquals (1, nodes.Count);=0A=
AssertSame (document.DocumentElement, nodes [0]);=0A=
}=0A=
+=0A=
+ public void TestNamespaceSelect()=0A=
+ {=0A=
+ XmlDocument document =3D new XmlDocument ();=0A=
+ document.LoadXml ("<root xmlns=3D\"urn:foo1:foo2\"/>");=0A=
+ XmlNamespaceManager nsmgr =3D new =
XmlNamespaceManager(document.NameTable);=0A=
+ nsmgr.AddNamespace("foons", "urn:foo1:foo2");=0A=
+ XmlNodeList nodes =3D document.SelectNodes ("/foons:root", nsmgr);=0A=
+ AssertEquals (1, nodes.Count);=0A=
+ }=0A=
}=0A=
}=0A=
------=_NextPart_000_01D0_01C2CFAE.B06DF310--