我有一个像这样的xml,如下所述。我试图使用以下表达式获得Cardnumber的值。
XPATH:
paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
但它给了我错误。任何人都能指导我吗?
<?xml version="1.0" encoding="UTF-8"?>
<paymentService version="1.0">
<ns0:submit xmlns:ns0="http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd">
<ns0:order>
<description>description</description>
<amount value="500" currencyCode="EUR" exponent="2"/>
<ns0:paymentDetails>
<ns0:VISA-SSL>
<cardNumber>00009875083428500</cardNumber>
<expiryDate>
<date month="02" year="2008"/>
</expiryDate>
<cardHolderName>test</cardHolderName>
</ns0:VISA-SSL>
<session shopperIPAddress="192.165.22.35" id=""/>
</ns0:paymentDetails>
<shopper>
<browser>
<acceptHeader>text/html</acceptHeader>
<userAgentHeader>mozilla 5.0</userAgentHeader>
</browser>
</shopper>
</ns0:order>
</ns0:submit>
</paymentService>
由于
答案 0 :(得分:0)
问题是节点
<paymentService version="1.0">
由于未结束,您必须对其进行评论或正确结束。 如果你评论试试这个XPATH
/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
答案 1 :(得分:0)
您的 xmlns:ns0
放错了位置,并且(想到这种方式)因为ns0
在 <ns0:submit>
标记之后定义为,ns0:submit
是“未定义”,因而是解析错误。
修改强>:
如果需要在PHP中使用此XPath,则必须在使用前声明命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<paymentService version="1.0" xmlns:ns0="http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd">
<ns0:submit>
或者在评估XPath之前注册命名空间(感谢@MiMo指出):
$xml->registerXPathNamespace("ns0","http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd");
在XPATH之前添加斜杠:
/paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber
Live demo with declaration first或Live demo with namespace registration(两者都在PHP中演示)。
答案 2 :(得分:0)
您需要在评估XPath之前注册命名空间:
$xml->registerXPathNamespace('ns0', 'http://www.tibco.com/ns/no_namespace_schema_location/Payment/PaymentProcessors/WorldPay_CC/SharedResources/Schemas/paymentService_v1.dtd');
其中$xml
是包含XML的SimpleXMLElement
变量。
根据Passerby的回答,您的XPath需要以/
开头:
/paymentService/ns0:submit/ns0:order/ns0:paymentDetails/ns0:VISA-SSL/cardNumber