简体   繁体   English

使用XPathFactory评估Xpath属性

[英]Evalute an Xpath attribute using XPathFactory

I'm trying to do something quite simple I think, simply assert that an attribute of an Xpath node is a specific value. 我想做的事情很简单,只需断言Xpath节点的属性是特定值即可。 The node has no value, onlya an attribute value as follows:- <ControlResponse Success="true"/> (will return "true" or "false") 该节点没有值,只有一个属性值,如下所示:- <ControlResponse Success="true"/> (将返回“ true”或“ false”)

<?xml version="1.0" encoding="UTF-8"?><tag0:AAA_ControlRS xmlns:tag0="http://www.xmltravel.com/fab/2002/09" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Target="test"     Version="2002A" xsi:type="AAA_ControlRS">
<tag0:TestInfo TestId="THFTEST"/>
<tag0:SessionInfo CreateNewSession="true"/>
<AAASessionId="8IzujBAVOPVQrO1ySpNBoJ9x"/>
<tag0:ControlResponse Success="true"/>
</tag0:AAA_ControlRS>

and here is my code: 这是我的代码:

//REQUEST
    String controlRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n...
//bunch of xml here";

   //RESPONSE
    String myControlResponse = given().
            when().
            request().
            contentType("text/xml").
            body(myControlRequest).
            when().post().andReturn().asString();

    //Parse response and get relevant node via Xpath
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    Document doc;

    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource((new StringReader(myControlResponse))));

        //Xpath Factory Object
        XPathFactory xPathFactory = XPathFactory.newInstance();

        //Xpath Object
        XPath xpath = xPathFactory.newXPath();

        String controlResponse = getNodeValue(doc, xpath);

        assertEquals("true", controlResponse);


    } catch (ParserConfigurationException | org.xml.sax.SAXException | IOException e) {
        e.printStackTrace();
    }
}

private static String getNodeValue(Document doc, XPath xpath) {
    String controlResponse = null;
    try {
        XPathExpression expr =
                xpath.compile("//ControlResponse/@Success");
        controlResponse = (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return controlResponse;
}

The xpath evaluates to null when I expect the String "true". 当我期望字符串“ true”时,xpath的计算结果为null。 I want to achieve getting the attribute value and asserting if it contains string "true" or "false" 我想获得属性值并断言它是否包含字符串“ true”或“ false”

Is there an easier way of achieving what I'm trying to do? 有没有更简单的方法可以实现我想要做的事情?

To get the attribute value , use //ControlResponse/@Success as the XPath expression. 要获取属性值 ,请使用//ControlResponse/@Success作为XPath表达式。

If namespace issues get in your way, use //*[local-name()="ControlResponse"]/@Success for a quick check, if the problem is namespace related. 如果遇到名称空间问题,请使用//*[local-name()="ControlResponse"]/@Success进行快速检查(如果问题与名称空间有关)。

Example using an unrelated sample document: 使用不相关样本文档的示例:

> cat ~/test.xml
<root><foo bar="true"/></root>
> xmllint --xpath '//foo/@bar' ~/test.xml
 bar="true"

If this doesn't work as expected in your case, please show enough of the XML document that the problem is reproducible. 如果您的情况无法按预期进行,请向XML文档显示足够的信息,指出该问题是可重现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM