public static XPathExpression buildXPathMatcherFromRules(String rules) throws XPathExpressionException {
      XPath xpath = XPathFactory.newInstance().newXPath();
      WritableNamespaceContext nsContext = new WritableNamespaceContext();

      // Parse SoapUI rules for getting namespaces and expression to evaluate.
      // declare namespace ser='http://www.example.com/test/service';
      // //ser:sayHello/name
      String xpathExpression = null;
      String lines[] = rules.split("\\r?\\n");
      for (String line : lines) {
         line = line.trim();
         if (line.startsWith("declare namespace ")) {
            String prefix = line.substring(18, line.indexOf("="));
            String namespace = line.substring(line.indexOf("=") + 2, line.lastIndexOf("'"));
            nsContext.addNamespaceURI(prefix, namespace);
         } else {
            xpathExpression = line;
         }
      }

      // Set namespace context and compile expression.
      xpath.setNamespaceContext(nsContext);
      return xpath.compile(xpathExpression);
   }
@Test
   void testBuildXPathMatcherFromRulesSimple() {

      String rules = "declare namespace ser='http://www.example.com/hello';\n" + "//ser:sayHello/name";
      String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:hel=\"http://www.example.com/hello\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <hel:sayHello>\n"
            + "         <name>Karla</name>\n" + "      </hel:sayHello>\n" + "   </soapenv:Body>\n"
            + "</soapenv:Envelope>";

      XPathExpression expression = null;

      try {
         expression = SoapUIXPathBuilder.buildXPathMatcherFromRules(rules);
      } catch (Throwable t) {
         fail("No exception should be thrown while parsing rules");
      }

      String result = null;
      try {
         result = expression.evaluate(new InputSource(new StringReader(soap)));
      } catch (Throwable t) {
         fail("No exception should be thrown while evaluating xpath");
      }
      assertEquals("Karla", result);
   }