1

I'm trying to return all the "field" children of the node "core" below:

<archive xmlns="http://rs.tdwg.org/dwc/text/" metadata="eml.xml">
  <core encoding="utf-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy="" ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Occurrence">
    <files>
      <location>occurrence.txt</location>
    </files>
    <id index="0" />
    <field index="1" term="http://rs.tdwg.org/dwc/terms/class"/>
    <field index="2" term="http://rs.tdwg.org/dwc/terms/maximumDepthInMeters"/>
    <field index="3" term="http://purl.org/dc/terms/language"/>
    <field index="4" term="http://rs.tdwg.org/dwc/terms/coordinatePrecision"/>
    <field index="5" term="http://rs.tdwg.org/dwc/terms/decimalLatitude"/>
 </core>
</archive

I have the following PHP:

$xml = new \DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->load($input_xml);

$xpath = new \DOMXpath($xml);
$xpath->registerNamespace('ns', $xml->documentElement->namespaceURI);

Something like this works for retrieving a specific "field" and getting it's index:

$query = $xpath->query("//ns:field[contains(@term, 'http://rs.tdwg.org/dwc/terms/class')]")->item(0);
$url = $query->attributes->getNamedItem("index")->nodeValue;

I've tried queries like the following using all sorts of examples on the net. None of them worked:

$children = $xpath->query("//ns:core/field");
$children = $xpath->query("//core/field");
$children = $xpath->query("/core/field");
$children = $xpath->query("/core/*");
$children = $xpath->query("/archive/core/field");
$children = $xpath->query("//ns:core/ns:field");

Ultimately, what I'm trying to achieve is returning all "field" nodes under "core", loop through them, and create an array from their index and term. For example:

$myArray = array(
    "1" => "http://rs.tdwg.org/dwc/terms/class",
    "2" => "http://rs.tdwg.org/dwc/terms/maximumDepthInMeters",
    "3" => "http://purl.org/dc/terms/language",
);

Any help would be appreciated. Thanks.

1 Answer 1

1

It might be the namespace that was tripping you up; you were also not querying the correct attribute (you were looking at index, but the URLs are in term). Try this code:

foreach( $xpath->query('/ns:archive/ns:core/ns:field') as $field) {
    if ( $field->attributes->getNamedItem("term")->nodeValue ) {
        echo "URL: " . $field->attributes->getNamedItem("term")->nodeValue . "\n";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Your xpath query got the info I needed. As for looking at the "index"... I only posted some other code I was using in another part to show xpath was working correctly.
I thought that was probably the case, but just thought I'd check. Glad that I could help!

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.