public void TwoLevelsOfElements() { List <Element> l = new List <Element>(); l.Add(new Element("foo")); l.Add(new Element("foo")); l.Add(new Element("bar")); l.Add(new Element("foo")); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.SetChildren(l); ctx.NavigateToChild(3); Assert.AreEqual("/foo[1]/foo[3]", ctx.XPath); Assert.AreEqual("/foo[1]", ctx.ParentXPath); ctx.NavigateToParent(); Assert.AreEqual("/foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/bar[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
public void ShouldCreateCopyOnClone() { List <Element> l = new List <Element>(); l.Add(new Element("foo")); l.Add(new Element("foo")); l.Add(new Element("bar")); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); XPathContext clone = (XPathContext)ctx.Clone(); Assert.AreEqual("/foo[2]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreNotSame(ctx, clone); clone.NavigateToParent(); clone.NavigateToChild(2); Assert.AreEqual("/bar[1]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
public void ElementsAndNs() { List <Element> l = new List <Element>(); l.Add(new Element("foo", "urn:foo:foo")); l.Add(new Element("foo")); l.Add(new Element("foo", "urn:foo:bar")); Dictionary <string, string> m = new Dictionary <string, string>(); m["bar"] = "urn:foo:bar"; XPathContext ctx = new XPathContext(m); ctx.SetChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/bar:foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
public void Init() { doc = new XmlDocument(); elements = new List <XmlNode>(); elements.Add(doc.CreateElement("foo")); elements.Add(doc.CreateElement("foo")); elements.Add(doc.CreateElement("bar")); elements.Add(doc.CreateElement("foo")); ctx = new XPathContext(); ctx.SetChildren(elements.Select(ElementSelectors.TO_NODE_INFO)); }
public void singleAttribute() { XPathContext ctx = new XPathContext(); ctx.SetChildren(Linqy.Singleton(new Element("foo"))); ctx.NavigateToChild(0); ctx.AddAttribute(new XmlQualifiedName("bar")); ctx.NavigateToAttribute(new XmlQualifiedName("bar")); Assert.AreEqual("/foo[1]/@bar", ctx.XPath); Assert.AreEqual("/foo[1]", ctx.ParentXPath); }
private Func <ComparisonState> CompareChildren(XPathContext controlContext, IEnumerable <XmlNode> controlChildren, XPathContext testContext, IEnumerable <XmlNode> testChildren) { return(() => { controlContext .SetChildren(controlChildren.Select <XmlNode, XPathContext.INodeInfo> (ElementSelectors.TO_NODE_INFO)); testContext .SetChildren(testChildren.Select <XmlNode, XPathContext.INodeInfo> (ElementSelectors.TO_NODE_INFO)); return CompareNodeLists(controlChildren, controlContext, testChildren, testContext); }); }
public void Mixed() { List <XPathContext.INodeInfo> l = new List <XPathContext.INodeInfo>(); l.Add(new Text()); l.Add(new Comment()); l.Add(new CDATA()); l.Add(new PI()); l.Add(new CDATA()); l.Add(new Comment()); l.Add(new PI()); l.Add(new Text()); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/text()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(1); Assert.AreEqual("/comment()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/text()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(3); Assert.AreEqual("/processing-instruction()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(4); Assert.AreEqual("/text()[3]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(5); Assert.AreEqual("/comment()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(6); Assert.AreEqual("/processing-instruction()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(7); Assert.AreEqual("/text()[4]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
private XPathContext GetXPathForNonAttribute(XmlNode n) { XmlNode parent = n.ParentNode; if (parent == null || parent is XmlDocument) { return(new XPathContext(prefix2uri, n)); } XPathContext parentContext = GetXPathForNonAttribute(parent); IEnumerable <XmlNode> children = parent.ChildNodes.Cast <XmlNode>(); parentContext.SetChildren(children.Select <XmlNode, XPathContext.INodeInfo>(ElementSelectors.TO_NODE_INFO)); ChildNodeXPathContextProvider cn = new ChildNodeXPathContextProvider(parentContext, children); return(cn.Map(n)); }
public void AttributesAndNs() { Dictionary <string, string> m = new Dictionary <string, string>(); m["bar"] = "urn:foo:bar"; XPathContext ctx = new XPathContext(m); ctx.SetChildren(Linqy.Singleton(new Element("foo", "urn:foo:bar"))); ctx.NavigateToChild(0); List <XmlQualifiedName> l = new List <XmlQualifiedName>(); l.Add(new XmlQualifiedName("baz")); l.Add(new XmlQualifiedName("baz", "urn:foo:bar")); ctx.AddAttributes(l); ctx.NavigateToAttribute(new XmlQualifiedName("baz")); Assert.AreEqual("/bar:foo[1]/@baz", ctx.XPath); Assert.AreEqual("/bar:foo[1]", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToAttribute(new XmlQualifiedName("baz", "urn:foo:bar")); Assert.AreEqual("/bar:foo[1]/@bar:baz", ctx.XPath); Assert.AreEqual("/bar:foo[1]", ctx.ParentXPath); ctx.NavigateToParent(); }