XPathUtils { public static Object evaluate(Node node, String xPathExpression, NamespaceContext nsContext, XPathExpressionResult resultType) { if (resultType.equals(XPathExpressionResult.NODE)) { Node resultNode = evaluateAsNode(node, xPathExpression, nsContext); if (resultNode.getNodeType() == Node.ELEMENT_NODE) { if (resultNode.getFirstChild() != null) { return resultNode.getFirstChild().getNodeValue(); } else { return ""; } } else { return resultNode.getNodeValue(); } } else if (resultType.equals(XPathExpressionResult.NODESET)) { NodeList resultNodeList = evaluateAsNodeList(node, xPathExpression, nsContext); List<String> values = new ArrayList<>(); for (int i = 0; i < resultNodeList.getLength(); i++) { Node resultNode = resultNodeList.item(i); if (resultNode.getNodeType() == Node.ELEMENT_NODE) { if (resultNode.getFirstChild() != null) { values.add(resultNode.getFirstChild().getNodeValue()); } else { values.add(""); } } else { values.add(resultNode.getNodeValue()); } } return values; } else if (resultType.equals(XPathExpressionResult.STRING)) { return evaluateAsString(node, xPathExpression, nsContext); } else { Object result = evaluateAsObject(node, xPathExpression, nsContext, resultType.getAsQName()); if (result == null) { throw new CitrusRuntimeException("No result for XPath expression: '" + xPathExpression + "'"); } if (resultType.equals(XPathExpressionResult.INTEGER)) { return (int) Math.round((Double) result); } return result; } } private  XPathUtils(); static Map<String, String> getDynamicNamespaces(String expression); static String replaceDynamicNamespaces(String expression, Map<String, String> namespaces); static boolean hasDynamicNamespaces(String expression); static Object evaluate(Node node, String xPathExpression,
            NamespaceContext nsContext, XPathExpressionResult resultType); static Node evaluateAsNode(Node node, String xPathExpression, NamespaceContext nsContext); static NodeList evaluateAsNodeList(Node node, String xPathExpression, NamespaceContext nsContext); static String evaluateAsString(Node node, String xPathExpression, NamespaceContext nsContext); static Boolean evaluateAsBoolean(Node node, String xPathExpression, NamespaceContext nsContext); static Double evaluateAsNumber(Node node, String xPathExpression, NamespaceContext nsContext); static Object evaluateAsObject(Node node, String xPathExpression, NamespaceContext nsContext, QName resultType); static boolean isXPathExpression(String expression); static Object evaluateExpression(Node node, String xPathExpression, NamespaceContext nsContext, QName returnType); static final String DYNAMIC_NS_START; static final String DYNAMIC_NS_END; }
@Test public void testEvaluate() { Document personNode = XMLUtils.parseMessagePayload("<person status=\"single\">" + "<name>foo</name>" + "<age>23</age>" + "</person>"); NamespaceContext namespaceContext = new SimpleNamespaceContext(); Assert.assertEquals(XPathUtils.evaluate(personNode, "/person/name", namespaceContext, XPathExpressionResult.STRING), "foo"); Assert.assertEquals(XPathUtils.evaluate(personNode, "/person/age", namespaceContext, XPathExpressionResult.NUMBER), 23.0D); Assert.assertEquals(XPathUtils.evaluate(personNode, "/person/age", namespaceContext, XPathExpressionResult.INTEGER), 23); Assert.assertEquals(XPathUtils.evaluateAsNode(personNode, "/person/name", namespaceContext).getFirstChild().getNodeValue(), "foo"); Assert.assertEquals(XPathUtils.evaluateAsNodeList(personNode, "/person/name", namespaceContext).item(0).getFirstChild().getNodeValue(), "foo"); Assert.assertEquals(XPathUtils.evaluateAsNodeList(personNode, "/person/unknown", namespaceContext).getLength(), 0L); Assert.assertTrue(XPathUtils.evaluateAsBoolean(personNode, "/person/name", namespaceContext)); Assert.assertFalse(XPathUtils.evaluateAsBoolean(personNode, "/person/unknown", namespaceContext)); Assert.assertEquals(XPathUtils.evaluateAsString(personNode, "/person/name", namespaceContext), "foo"); Assert.assertEquals(XPathUtils.evaluateAsObject(personNode, "/person/name", namespaceContext, new QName("http: Assert.assertEquals(XPathUtils.evaluateAsNumber(personNode, "/person/age", namespaceContext), new Double(23.0D)); Assert.assertEquals(XPathUtils.evaluateAsString(personNode, "/person/@status", namespaceContext), "single"); }