xpath soap名称空间抓取项

问题描述

| 我觉得我做的事情确实不正确。 做肥皂时,他们给我返回一个可能包含或不包含错误的xml。 如果不读取值,我想检查错误是否存在。 无论如何,我无法直接抓住它:( 以下是带有结果的内容的示例返回,其中一个给出了错误(未找到名称
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soapEnvelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">
    <envHeader xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">
        <wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
        <wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
        <wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
        <wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
        <wsseSecurity>
        <wsuTimestamp wsu:Id=\"Timestamp-061df7b5-32a2-4021-852d-2df98953e076\">
            <wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
            <wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
        </wsuTimestamp>
    </envHeader>
    <soapBody>
        <searchPersonResponse xmlns=\"http://www.rechtspraak.nl/namespaces/ccr01\">
            <searchPersonResult>
                <CCR_WS xmlns=\"http://www.rechtspraak.nl/namespaces/ccr\">
                    <curandus>
                        <ccn>1</ccn>
                        <cur_voornamen>Jan</cur_voornamen>
                        <cur_voorvoegsels>van</cur_voorvoegsels>
                        <cur_achternaam>Beek</cur_achternaam>
                        <geboorte_datum>1980-01-02</geboorte_datum>
                        <geboorte_plaats>Werkendam</geboorte_plaats>
                    </curandus>
                </CCR_WS>
            </searchPersonResult>
        </searchPersonResponse>
    </soapBody>
</soapEnvelope>
一个没有结果
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soapEnvelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">
    <envHeader xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">
        <wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
        <wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
        <wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
        <wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
        <wsseSecurity>
        <wsuTimestamp wsu:Id=\"Timestamp-061df7b5-32a2-4021-852d-2df98953e076\">
            <wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
            <wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
        </wsuTimestamp>
    </envHeader>
    <soapBody>
        <searchPersonResponse xmlns=\"http://www.rechtspraak.nl/namespaces/ccr01\">
            <searchPersonResult>
                <CCR_WS xmlns=\"http://www.rechtspraak.nl/namespaces/ccr\">
                    <exceptie errorcode=\"1\">No Results found.</exceptie>
                </CCR_WS>
            </searchPersonResult>
        </searchPersonResponse>
    </soapBody>
</soapEnvelope>
这是我的代码,选择名称空间,然后检查
$results = simplexml_load_string($response);
$results->registerXPathNamespace(\'ccr\',\'http://www.rechtspraak.nl/namespaces/ccr\');

$lijst = $results->xpath(\'//ccr:CCR_WS\');
$errorcode  = $lijst[0]->exceptie->attributes()->errorcode;
$error      = $lijst[0]->exceptie;
if (isset($errorcode) AND $errorcode != \"\") {
    // do things with the error code
} else {
    $lijst = $results->xpath(\'//ccr01:searchPersonResult\'); 
    $cur = $lijst[0]->CCR_WS->curandus;
    echo $cur->ccn.\"<BR>\";
    echo $cur->cur_voornamen.\"<BR>\";
    echo $cur->cur_voorvoegsels.\"<BR>\";
    echo $cur->cur_achternaam.\"<BR>\";
    echo $cur->geboorte_datum.\"<BR>\";
    echo $cur->geboorte_plaats.\"<BR>\";
}
肯定有更好的抓取方式 $ lijst [0]-> exceptie-> attributes()->错误代码 例如...     

解决方法

        ...不知道这是否是所有人的“更好方法”,但这是直接使用XPath表达式选择错误代码。通过放下步骤并使用
//
(在开头或中间),可以使它更短且效率更低。使用
@
选择属性(如果需要较长的语法,则选择
attribute::
轴)。如果属性(或exceptionie元素)不存在,则不返回任何内容。
/*/*/ccr01:searchPersonResponse/ccr01:searchPersonResult/ccr:CCR_WS/ccr:exceptie/@errorcode
请记住注册您在XPath表达式中使用的所有名称空间前缀。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...