0

i try to geht the data from a xml file and i have troubles to get data, for example, how can i get the caaml:locRef value or the caaml:beginPosition value?

here is the code so far:

/* a big thank you to helderdarocha */
/* – he already helped me yesterday with a part of this code */ 

$doc = new DOMDocument();
    $doc->load('xml/test.xml');

    $xpath = new DOMXpath($doc);
    $xpath->registerNamespace("caaml", "http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS");

    if ($doc->schemaValidate('http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS/CAAMLv5_BulletinEAWS.xsd')) {

        foreach ($xpath->query('//caaml:DangerRating') as $key) {
            echo $key->nodeValue;
            print_r($key);
        }

    } 

and here ist the print_r from $key

DOMElement Object ( [tagName] => caaml:DangerRating [schemaTypeInfo] => [nodeName] => caaml:DangerRating [nodeValue] => 2014-03-03+01:00 2 [nodeType] => 1 [parentNode] => (object value omitted) [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => (object value omitted) [nextSibling] => (object value omitted) [attributes] => (object value omitted) [ownerDocument] => (object value omitted) [namespaceURI] => http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS [prefix] => caaml [localName] => DangerRating [baseURI] => /Applications/MAMP/htdocs/lola/xml/test.xml [textContent] => 2014-03-03+01:00 2 ) 2014-03-04+01:00 2 

and here a part of the xml

 <caaml:DangerRating>
      <caaml:locRef xlink:href="AT7R1"/>
      <caaml:validTime>
        <caaml:TimePeriod>
          <caaml:beginPosition>2014-03-06T00:00:00+01:00</caaml:beginPosition>
          <caaml:endPosition>2014-03-06T11:59:59+01:00</caaml:endPosition>
        </caaml:TimePeriod>
      </caaml:validTime>
      <caaml:validElevation>
        <caaml:ElevationRange uom="m">
          <caaml:beginPosition>2200</caaml:beginPosition>
        </caaml:ElevationRange>
      </caaml:validElevation>
      <caaml:mainValue>2</caaml:mainValue>
    </caaml:DangerRating>
    <caaml:DangerRating>
      <caaml:locRef xlink:href="AT7R1"/>
      <caaml:validTime>
        <caaml:TimePeriod>

thanks!

1 Answer 1

2

The second argument of DOMXpath::query()/evaluate() is the context for the expression. You already select and iterate the DangerRating. Inside the loop you can use additional expressions to fetch the data:

foreach ($xpath->evaluate('//caaml:DangerRating') as $dangerRating) {
  echo $xpath->evaluate(
    'string(caaml:validTime/caaml:TimePeriod/caaml:beginPosition)'
    $dangerRating
  );
}

The string() Xpath function casts the first node of the location path into a string. This way you don't need the ->item(0)->nodeValue in PHP. It will only work with evaluate().

The caaml:locRef value is stored in an attribute in a different namespace. So you will need to register that namespace first:

$xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");

After that you can fetch and cast the attribute node:

$locRef = $xpath->evaluate(
 'string(caaml:locRef/@xlink:href)', $dangerRating
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.