0

我有来自 Ebay 的以下 XML 文件

<?xml version="1.0"?>
<GeteBayDetailsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2012-07-04T12:02:14.541Z</Timestamp>
    <Ack>Success</Ack>
    <Version>779</Version>
    <Build>E779_INTL_BUNDLED_14986004_R1</Build>
    <SiteDetails>
        <Site>US</Site>
        <SiteID>0</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Canada</Site>
        <SiteID>2</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>UK</Site>
        <SiteID>3</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Germany</Site>
        <SiteID>77</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Australia</Site>
        <SiteID>15</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>France</Site>
        <SiteID>71</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>eBayMotors</Site>
        <SiteID>100</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Italy</Site>
        <SiteID>101</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Netherlands</Site>
        <SiteID>146</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Spain</Site>
        <SiteID>186</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>India</Site>
        <SiteID>203</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>HongKong</Site>
        <SiteID>201</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Singapore</Site>
        <SiteID>216</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Malaysia</Site>
        <SiteID>207</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Philippines</Site>
        <SiteID>211</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>CanadaFrench</Site>
        <SiteID>210</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Poland</Site>
        <SiteID>212</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Belgium_Dutch</Site>
        <SiteID>123</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Belgium_French</Site>
        <SiteID>23</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Austria</Site>
        <SiteID>16</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Switzerland</Site>
        <SiteID>193</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <SiteDetails>
        <Site>Ireland</Site>
        <SiteID>205</SiteID>
        <DetailVersion>1</DetailVersion>
        <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
    </SiteDetails>
    <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</GeteBayDetailsResponse>

我需要获取节点 SiteID 的值,其中节点 Site 与我传递的任何变量匹配。我的代码是:

$xml_file ='SiteDetails.xml';
$xmlDoc = new DomDocument();
$xmlDoc->load($xml_file);
$xpath = new DOMXpath($xmlDoc);

$siteIDList =  $xpath->query("/GeteBayDetailsResponse/SiteDetails[Site=$site]/SiteID");

var_dump($siteIDList);
echo $siteIDList->SiteID;

我得到以下结果:

object(DOMNodeList)#11 (0) {
}

任何人都可以帮忙吗?我想获得 3 的值。

4

2 回答 2

0

编辑:使用 Florent 回答中的 API registerNamespace

您的 XML 文件包含默认命名空间。ebay因此,您可以在代码中针对默认命名空间 ( )注册一个变量名称,urn:ebay:apis:eBLBaseComponents如下所示,并使用以下 xpath

$xpath->registerNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');

$siteIDList = $xpath->query("//ebay:Site[text()='$site']/following-sibling::ebay:SiteID/text()");

或者

您可以使用以下 XPATH,除了评估它之外不需要任何额外的代码:-)

//*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='Site' and text()='$site']/following-sibling::*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='SiteID']/text()

注意: $site 是要匹配的<Site>节点的运行时值。<SiteID>

于 2012-07-04T14:52:41.643 回答
0

我认为你必须$site用引号括起来并添加命名空间:

$xpath = new DOMXPath($xmlDoc);
$xpath->registerNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');

$siteIDList = $xpath->query("/ebay:GeteBayDetailsResponse/ebay:SiteDetails[ebay:Site='$site']/ebay:SiteID");

只是为了确定,能给我们$site提供这个结果的值吗?

编辑:按照 Vaman Kulkarni 的建议添加了命名空间。

于 2012-07-04T14:37:49.700 回答