📄 basexpathtest.java
字号:
a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); List result = xpath.selectNodes(doc); assertEquals(4, result.size()); assertEquals(x1, result.get(0)); assertEquals(x2, result.get(1)); assertEquals(x3, result.get(2)); assertEquals(x4, result.get(3)); } // test case for JAXEN-55 public void testDescendantAxis() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); List result = xpath.selectNodes(doc); assertEquals(4, result.size()); assertEquals(x1, result.get(0)); assertEquals(x2, result.get(1)); assertEquals(x3, result.get(2)); assertEquals(x4, result.get(3)); } public void testDescendantAxisWithAttributes() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x/@*"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); Attr a1 = doc.createAttribute("name"); a1.setNodeValue("1"); x1.setAttributeNode(a1); Attr a2 = doc.createAttribute("name"); a2.setNodeValue("2"); x2.setAttributeNode(a2); Attr a3 = doc.createAttribute("name"); a3.setNodeValue("3"); x3.setAttributeNode(a3); Attr a4 = doc.createAttribute("name"); a4.setNodeValue("4"); x4.setAttributeNode(a4); List result = xpath.selectNodes(doc); assertEquals(4, result.size()); assertEquals(a1, result.get(0)); assertEquals(a2, result.get(1)); assertEquals(a3, result.get(2)); assertEquals(a4, result.get(3)); } public void testDescendantAxisWithNamespaceNodes() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x/namespace::node()"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); Attr a1 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:a"); a1.setNodeValue("http://www.example.org/"); x1.setAttributeNode(a1); Attr a2 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:b"); a2.setNodeValue("http://www.example.org/"); x2.setAttributeNode(a2); Attr a3 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:c"); a3.setNodeValue("http://www.example.org/"); x3.setAttributeNode(a3); Attr a4 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:d"); a4.setNodeValue("http://www.example.org/"); x4.setAttributeNode(a4); List result = xpath.selectNodes(doc); assertEquals(8, result.size()); Iterator iterator = result.iterator(); StringBuffer sb = new StringBuffer(4); while (iterator.hasNext()) { NamespaceNode ns = (NamespaceNode) iterator.next(); if (ns.getNodeValue().equals("http://www.example.org/")) { String name = ns.getNodeName(); sb.append(name); } } assertEquals("abcd", sb.toString()); } public void testMultipleAttributesOnElement() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x/@*"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); a.appendChild(x1); a.appendChild(b); Attr a1 = doc.createAttribute("name1"); a1.setNodeValue("1"); x1.setAttributeNode(a1); Attr a2 = doc.createAttribute("name2"); a2.setNodeValue("2"); x1.setAttributeNode(a2); Attr a3 = doc.createAttribute("name3"); a3.setNodeValue("3"); x1.setAttributeNode(a3); Attr a4 = doc.createAttribute("name4"); a4.setNodeValue("4"); x1.setAttributeNode(a4); List result = xpath.selectNodes(doc); assertEquals(4, result.size()); assertTrue(result.contains(a1)); assertTrue(result.contains(a2)); assertTrue(result.contains(a3)); assertTrue(result.contains(a4)); } public void testXMLNamespaceAttributeOrderOnAncestorAxis() throws JaxenException { org.w3c.dom.Element superroot = doc.createElement("superroot"); doc.appendChild(superroot); org.w3c.dom.Element root = doc.createElement("root"); superroot.appendChild(root); org.w3c.dom.Attr p0 = doc.createAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:id"); p0.setValue("p0"); superroot.setAttributeNodeNS(p0); org.w3c.dom.Attr p1 = doc.createAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:id"); p1.setValue("p1"); root.setAttributeNodeNS(p1); org.w3c.dom.Element child = doc.createElement("child312"); root.appendChild(child); BaseXPath xpath = new DOMXPath("ancestor::*/@xml:*"); List result = xpath.selectNodes(child); assertEquals(2, result.size()); assertEquals(p0, result.get(0)); assertEquals(p1, result.get(1)); } public void testDescendantAxisWithAttributesAndChildren() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x/@* | /descendant::x"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); Attr a1 = doc.createAttribute("name"); a1.setNodeValue("1"); x1.setAttributeNode(a1); Attr a2 = doc.createAttribute("name"); a2.setNodeValue("2"); x2.setAttributeNode(a2); Attr a3 = doc.createAttribute("name"); a3.setNodeValue("3"); x3.setAttributeNode(a3); Attr a4 = doc.createAttribute("name"); a4.setNodeValue("4"); x4.setAttributeNode(a4); List result = xpath.selectNodes(doc); assertEquals(8, result.size()); assertEquals(x1, result.get(0)); assertEquals(a1, result.get(1)); assertEquals(x2, result.get(2)); assertEquals(a2, result.get(3)); assertEquals(x3, result.get(4)); assertEquals(a3, result.get(5)); assertEquals(x4, result.get(6)); assertEquals(a4, result.get(7)); } public void testAncestorAxisWithAttributes() throws JaxenException { BaseXPath xpath = new DOMXPath("ancestor::*/@*"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); a.appendChild(b); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); b.appendChild(x3); Attr a1 = doc.createAttribute("name"); a1.setNodeValue("1"); a.setAttributeNode(a1); Attr a2 = doc.createAttribute("name"); a2.setNodeValue("2"); b.setAttributeNode(a2); Attr a3 = doc.createAttribute("name"); x3.setNodeValue("3"); x3.setAttributeNode(a3); List result = xpath.selectNodes(x3); assertEquals(2, result.size()); assertEquals(a1, result.get(0)); assertEquals(a2, result.get(1)); } // test for Jaxen-83 public void testPrincipalNodeTypeOfSelfAxisIsElement() throws JaxenException { BaseXPath xpath = new DOMXPath("child/@*[self::test]"); org.w3c.dom.Element a = doc.createElementNS("", "child"); org.w3c.dom.Attr test = doc.createAttributeNS("", "test"); test.setValue("value"); a.setAttributeNode(test); doc.appendChild(a); List result = xpath.selectNodes(doc); assertEquals(0, result.size()); } // test to make sure Jaxen-83 fix doesn't go too far public void testSelfAxisWithNodeTestCanReturnNonPrincipalNodeType() throws JaxenException { BaseXPath xpath = new DOMXPath("child/@*[self::node()]"); org.w3c.dom.Element a = doc.createElementNS("", "child"); org.w3c.dom.Attr test = doc.createAttributeNS("", "test"); test.setValue("value"); a.setAttributeNode(test); doc.appendChild(a); List result = xpath.selectNodes(doc); assertEquals(1, result.size()); } // another Jaxen-55 test to try to pin down exactly what does // and doesn't work public void testDescendantOrSelfAxis() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant-or-self::x"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); List result = xpath.selectNodes(doc); assertEquals(4, result.size()); assertEquals(x1, result.get(0)); assertEquals(x2, result.get(1)); assertEquals(x3, result.get(2)); assertEquals(x4, result.get(3)); } public void testDuplicateNodes() throws JaxenException { BaseXPath xpath = new DOMXPath("//x | //*"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); List result = xpath.selectNodes(doc); assertEquals(6, result.size()); } public void testUnionOfNodesWithNonNodes() throws JaxenException { BaseXPath xpath = new DOMXPath("count(//*) | //x "); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); try { xpath.selectNodes(doc); fail("Allowed union with non-node-set"); } catch (JaxenException ex) { assertNotNull(ex.getMessage()); } } public void testUnionOfEmptyNodeSetWithNonNodes() throws JaxenException { BaseXPath xpath = new DOMXPath("//y | count(//*)"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); b.appendChild(x2); x2.appendChild(doc.createTextNode("2")); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -