Assert that a specific XPath does NOT exist in some given XML
@param xPathExpression
@param inDocument
@see XpathEngine which provides the underlying evaluation mechanism
969476
public static void assertXpathNotExists(String xPathExpression, 
                                            Document inDocument)
        throws XpathException {
        XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
        NodeList nodeList = simpleXpathEngine.getMatchingNodes(
                                                               xPathExpression, inDocument);
        int matches = nodeList.getLength();
        assertEquals("Should be zero matches for Xpath " + 
                     xPathExpression, 0, matches);
    }
593487
public final void xpathSeparator() throws RecognitionException {
		try {
			// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:560:5: ( DIV | QUESTION_DIV )
			// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:
			{
			if ( input.LA(1)==DIV||input.LA(1)==QUESTION_DIV ) {
				input.consume();
				state.errorRecovery=false;
				state.failed=false;
			}
			else {
				if (state.backtracking>0) {state.failed=true; return;}
				MismatchedSetException mse = new MismatchedSetException(null,input);
				throw mse;
			}
			}

		}

		catch (RecognitionException re) {
		    throw re;
		}

		finally {
			// do for sure before leaving
		}
	}
950642
public static Node getFirstNode(Document doc, String xPathExpression) {
			try {

				XPathFactory xPathfactory = XPathFactory.newInstance();
				XPath xpath = xPathfactory.newXPath();
				XPathExpression expr;
				expr = xpath.compile(xPathExpression);
				NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
				return nl.item(0);

			} catch (XPathExpressionException e) {
				e.printStackTrace();
			}

			return null;
		}
9057
protected void compareNode(Node control, Node test,
                               DifferenceListener listener, ElementQualifier elementQualifier) 
        throws DifferenceFoundException {
        boolean comparable = compareNodeBasics(control, test, listener);
        boolean isDocumentNode = false;

        if (comparable) {
            switch (control.getNodeType()) {
            case Node.ELEMENT_NODE:
                compareElement((Element)control, (Element)test, listener);
                break;
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
                compareText((CharacterData) control,
                            (CharacterData) test, listener);
                break;
            case Node.COMMENT_NODE:
                compareComment((Comment)control, (Comment)test, listener);
                break;
            case Node.DOCUMENT_TYPE_NODE:
                compareDocumentType((DocumentType)control,
                                    (DocumentType)test, listener);
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                compareProcessingInstruction((ProcessingInstruction)control,
                                             (ProcessingInstruction)test, listener);
                break;
            case Node.DOCUMENT_NODE:
                isDocumentNode = true;
                compareDocument((Document)control, (Document) test, 
                                listener, elementQualifier);
                break;
            default:
                listener.skippedComparison(control, test);
            }
        } 

        compareHasChildNodes(control, test, listener);
        if (isDocumentNode) {
            Element controlElement = ((Document)control).getDocumentElement();
            Element testElement = ((Document)test).getDocumentElement();
            if (controlElement!=null && testElement!=null) {
                compareNode(controlElement, testElement, listener, elementQualifier);
            }
        } else {
            controlTracker.indent();
            testTracker.indent();
            compareNodeChildren(control, test, listener, elementQualifier);
            controlTracker.outdent();
            testTracker.outdent();
        }
    }
1156213
public static void assertEquals(String message, XmlCursor expected, XmlCursor actual)
  {
    for (int child = 0; true; child++)
    {
      boolean child1 = expected.toChild(child);
      boolean child2 = actual.toChild(child);
      if (child1 != child2)
      {
        fail(message, "Different XML structure near " + QNameHelper.pretty(expected.getName()));
      }
      else if (expected.getName() != null && !expected.getName().equals(actual.getName()))
      {
        fail(message, "Expected element: '" + expected.getName()
          + "' differs from actual element: '" + actual.getName() + "'");
      }
      else if (child == 0 && !child1)
      {
        if (!(expected.getTextValue().equals(actual.getTextValue())))
        {
          fail(message, "Expected value for element " + QNameHelper.pretty(expected.getName()) + " -> '"
            + expected.getTextValue() + "' differs from actual value '" + actual.getTextValue() + "'");
        }
        break;
      }
      else if (child1)
      {
        assertEquals(message, expected, actual);
        expected.toParent();
        actual.toParent();
      }
      else
      {
        break;
      }
    }

    assertAttributesEqual(message, expected, actual);
  }
83010
public boolean find(String xmlPathQuery) {
        XmlPath xmlPath = XmlPathParser.parse(xmlPathQuery);

        XmlNode node;
        while((node = pullXmlNode()) != null) {
            if(node instanceof XmlStartElement) {
                XmlStartElement startElement = (XmlStartElement) node;
                Element element = new Element(startElement.getLocalName());
                element.addAttributes(startElement.getAttributes());
                currentPath.addLast(element);
                if(xmlPath.matches(currentPath)) {
                    nodeQueue.push(node);
                    currentPath.removeLast();
                    return true;
                }
            } else if(node instanceof XmlEndElement) {
                if(currentPath.getLast() instanceof Content) {
                    currentPath.removeLast();
                }
                currentPath.removeLast();
            } else if(node instanceof XmlContent) {
                XmlContent content = (XmlContent) node;
                if(currentPath.getLast() instanceof Content) {
                    currentPath.removeLast();
                }
                currentPath.addLast(new Content(content.getText()));
            } else {
                throw new XmlReaderException("Unknown XmlNode type: " + node);
            }
        }
        return false;
    }
183466
public static void assertXpathEvaluatesTo(QualifiedName expectedValue,
                                              String xpathExpression,
                                              Document inDocument)
        throws XpathException {
        XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
        NodeList nl =
            simpleXpathEngine.getMatchingNodes(xpathExpression, inDocument);
        NamespaceContext ctx = null;
        if (nl.getLength() > 0) {
            ctx = new NodeBasedNamespaceContext(nl.item(0));
        }
        assertEquals(expectedValue,
                     QualifiedName
                     .valueOf(simpleXpathEngine.evaluate(xpathExpression,
                                                         inDocument),
                              ctx));
    }
1339557
public static void ensureXPathNotNull(Node node, String expression) {
    if (node == null) {
      throw LOG.unableToFindXPathExpression(expression);
    }
  }
962547
JavaXmlQuery compile(XPath xpath) {
        try {
            this.expression = xpath.compile(getQuery());
        } catch (XPathExpressionException e) {
            LOGGER.error("Cannot compile XPath query: " + getQuery(), e);
        }
        return this;
    }
402338
private static boolean isAttributeIgnored(Node receivedElement, Node receivedAttribute,
                                             Set<String> ignoreMessageElements, NamespaceContext namespaceContext) {
        if (CollectionUtils.isEmpty(ignoreMessageElements)) {
            return false;
        }

        /** This is the faster version, but then the ignoreValue name must be
         * the full path name like: Numbers.NumberItem.AreaCode
         */
        if (ignoreMessageElements.contains(XMLUtils.getNodesPathName(receivedElement) + "." + receivedAttribute.getNodeName())) {
            return true;
        }

        /** This is the slower version, but here the ignoreValues can be
         * the short path name like only: AreaCode
         *
         * If there are more nodes with the same short name,
         * the first one will match, eg. if there are:
         *      Numbers1.NumberItem.AreaCode
         *      Numbers2.NumberItem.AreaCode
         * And ignoreValues contains just: AreaCode
         * the only first Node: Numbers1.NumberItem.AreaCode will be ignored.
         */
        for (String expression : ignoreMessageElements) {
            Node foundAttributeNode = XMLUtils.findNodeByName(receivedElement.getOwnerDocument(), expression);

            if (foundAttributeNode != null && receivedAttribute.isSameNode(foundAttributeNode)) {
                return true;
            }
        }

        /** This is the XPath version using XPath expressions in
         * ignoreValues to identify nodes to be ignored
         */
        for (String expression : ignoreMessageElements) {
            if (XPathUtils.isXPathExpression(expression)) {
                Node foundAttributeNode = XPathUtils.evaluateAsNode(receivedElement.getOwnerDocument(),
                        expression,
                        namespaceContext);
                if (foundAttributeNode != null && foundAttributeNode.isSameNode(receivedAttribute)) {
                    return true;
                }
            }
        }

        return false;
    }
430488
public static void assertXMLEqual(InputSource control, InputSource test)
        throws SAXException, IOException {
        assertXMLEqual(null, control, test);
    }
76754
public static void assertXpathValuesNotEqual(String controlXpath,
                                                 String inControlXMLString,
                                                 String testXpath,
                                                 String inTestXMLString)
        throws SAXException, IOException,
               XpathException {
        assertXpathValuesNotEqual(controlXpath,
                                  XMLUnit.buildControlDocument(inControlXMLString),
                                  testXpath,
                                  XMLUnit.buildTestDocument(inTestXMLString));
    }
209619
public static void assertXpathEvaluatesTo(String expectedValue,
                                              String xpathExpression,
                                              Document inDocument)
        throws XpathException {
        XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
        assertEquals(expectedValue,
                     simpleXpathEngine.evaluate(xpathExpression, inDocument));
    }
303404
public String verifyXPathExpression(final Iterable<XmlNsScheme> xmlNsSchemes, final String xpathExpression)
    {
        LOG.trace("verifyXPathExpression: {}", xpathExpression);
        final XPathCompiler xPathCompiler = processor.newXPathCompiler();
        for (final XmlNsScheme xmlNsScheme : xmlNsSchemes)
        {
            final String localPart = xmlNsScheme.getQName().getLocalPart();
            final String namespaceUri = xmlNsScheme.getQName().getNamespaceURI();
            LOG.trace("declareNamespace {}:{}", localPart, namespaceUri);
            xPathCompiler.declareNamespace(localPart, namespaceUri);
        }
        try
        {
            xPathCompiler.compile(xpathExpression);
        }
        catch (final SaxonApiException e)
        {
            return e.getCause().getMessage();
        }
        return "";
    }
781152
@SuppressWarnings("unchecked")
    public static List<Node> selectNodes(Document doc, String xpathExpression) {

        return doc.selectNodes(xpathExpression);
    }
911609
private IdentifierImpl createIdentifierForWildExpression(String selector) 
    throws InvalidXPathSyntaxException 
  {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      tc.entry(cclass,
               "createIdentifierForWildExpression",
               "selector: " + selector);
    
    IdentifierImpl wildIdentifier = new IdentifierImpl(selector); 

    // Set the appropriate XPath parameters for this Identifier
    setXPathCharacteristics(wildIdentifier); 
    
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      tc.exit(this,cclass, "createIdentifierForSubExpression", wildIdentifier); 
    
    return wildIdentifier;
  }
968574
public void validateStringWithXmlSchema(String xml, String xmlSchemaPath) throws IOException {
    try {
      URL schemaUrl = this.getClass().getResource(xmlSchemaPath);
      if (schemaUrl == null) {
        throw new IOException("XML schema not found in classpath: " + xmlSchemaPath);
      }
      try (ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("utf-8"))) {
        validateInternal(new StreamSource(stream), schemaUrl);
      }
    } catch (SAXException e) {
      throw new RuntimeException(e);
    }
  }