2

Im trying to navigate an XML block similar to this one ($doc) using PHP simplexml_load_string and using xpath on $doc to get only the 'Day' block like this:

$myday = $doc->xpath ('//Day');

that lets me access all data from the block as an object, meaning

$myday->AdultCount;

returns 1 and

$myday->Id;

returns "6a0"

however I can't access "SpecialDeals" content not using:

$myday->SpecialDeals

nor using:

$myday->SpecialDeals->a:string

Whats is the right syntax in this case?

<Days>
  <DaysId>687</DaysId>
    <Day>
      <AdultsCount>1</AdultsCount>
      <Availability>Available</Availability>
      <Id>6a0</Id>
      <RoomType>Studio</RoomType>
      <SpecialDeals xmlns:a="http://microsoft.com/2003/Arrays">
        <a:string>Best Day Ever</a:string>
      </SpecialDeals>
    </Day>
  <DaysPrice>247.4</DaysPrice>
</Days>");
paoloi
  • 189
  • 2
  • 6
  • 19
  • 1
    You are not aware so far that `a:` denotes an XML Namespace Prefix in XML and that you need to specify the namespace next to the element name in SimpleXML unless it's in the default namespace. This is a common question and has been asked before, let me fetch you an existing answer. – hakre Oct 24 '13 at 16:00
  • 1
    And keep in mind that there are no *special characters*, that is a misnomer. All characters are equal. ;) – hakre Oct 24 '13 at 16:05

1 Answers1

2

You can access the tags with colons in them (aka namespaces) using the children() method:

echo $xml->Day->SpecialDeals->children('a', true)->string[0];

Demo!

This SitePoint article explains namespaces in detail.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150