简体   繁体   English

使用PHP中具有多个名称空间的XML内容检索值的数组

[英]Retrieving an array of values from an XML content with multiple namespaces in PHP

I posted another question, which to my error was incomplete. 我发布了另一个问题,对于我的错误,此问题是不完整的。 I have an XML with multiple namespaces, but I need to access the values of in a foreach. 我有一个带有多个命名空间的XML,但是我需要在foreach中访问的值。

My XML array is: 我的XML数组是:

<ArrayOfSession xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/Vista.Online.BackOffice.Api.Models.V1'> 
<Session>
  <AreComplimentariesAllowed>true</AreComplimentariesAllowed>
  <Attributes xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d3p1:string>0000000009</d3p1:string>
    <d3p1:string>0000000011</d3p1:string>
  </Attributes>
</Session>
</ArrayOfSession>

I was helped by another user at: Retrieving an array of values from an XML content with a namespace in PHP 在以下位置,我得到了另一个用户的帮助: 在PHP中使用命名空间从XML内容检索值的数组

With that answer I have tried a few variations. 有了这个答案,我尝试了一些变化。 This being my current one, which isn't working: 这是我当前的那个,它不起作用:

$xml->registerXPathNamespace('i', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('j', 'http://schemas.datacontract.org/2004/07/Vista.Online.BackOffice.Api.Models.V1');
$xml->registerXPathNamespace('ns', 'http://schemas.microsoft.com/2003/10/Serialization/Arrays');

foreach($xml->xpath('//ns:d3p1') as $header){
    var_export($header->xpath('//Attributes/ns:string'));
}

I also tried: 我也尝试过:

foreach($xml->xpath('//ns:d3p1') as $header){
        var_export($header->xpath('//ns:string'));
    }

This is stumping me! 这真让我难过!

In my current working version for the rest of code, I'm retrieving the elements with: 在其余代码的当前工作版本中,我正在使用以下方法检索元素:

foreach($xml->Session as $event){
    // Do Something with $event->AreComplimentariesAllowed
}

This works fine, but I just can't access those Attributes elements. 这工作正常,但是我无法访问那些Attributes元素。

Thanks as ever. 一如既往的感谢。

You have to register the namespaces it on each SimpleXMLElement before you call xpath() . 在调用xpath()之前,必须在每个SimpleXMLElement上注册它的名称空间。 You register them on the $xml variable, but not on $header . 您可以在$xml变量上注册它们,但不能在$header上注册它们。

Your XML has an default namespace, too. 您的XML也具有默认名称空间。 You do register the prefix j for it, but you do not use the prefix in the expressions. 您确实为其注册了前缀j ,但是您没有在表达式中使用前缀。 Last here are no elements d3p1 . 最后,这里没有元素d3p1 This is only a namespace prefix in the document. 这只是文档中的名称空间前缀。

So to iterate the Session elements and then the string values inside the Attributes 因此,要迭代Session元素,然后迭代Attributes内部的string

$sessions = new SimpleXMLElement($xml);
$sessions->registerXPathNamespace(
  'j', 
  'http://schemas.datacontract.org/2004/07/Vista.Online.BackOffice.Api.Models.V1'
);

foreach($sessions->xpath('//j:Session') as $session) {
  $session->registerXPathNamespace(
    'j', 
    'http://schemas.datacontract.org/2004/07/Vista.Online.BackOffice.Api.Models.V1'
  );
  $session->registerXPathNamespace(
    'ns', 
    'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
  );
  var_export($session->xpath('.//j:Attributes/ns:string'));
}

In DOM this looks different: 在DOM中,这看起来有所不同:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
  'j', 
  'http://schemas.datacontract.org/2004/07/Vista.Online.BackOffice.Api.Models.V1'
);
$xpath->registerNamespace(
  'ns', 
  'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
);

foreach ($xpath->evaluate('//j:Session') as $session) {
  var_export(
    iterator_to_array($xpath->evaluate('.//j:Attributes/ns:string', $session))
  );
}

It has a dedicated Xpath object, so the namespace registration has to be done only once. 它具有专用的Xpath对象,因此名称空间注册仅需执行一次。

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

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