سؤال

I'm using SimpleXML to go through a kml doc, and I'm working on a project where I need to retrieve attributes in a specific order.

If I had something like this:

<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#mizipirs">
    <SimpleData name="ZCTA5CE10">55555</SimpleData>
    <SimpleData name="GEOID10">55555</SimpleData>
    <SimpleData name="CLASSFP10">XX</SimpleData>
    <SimpleData name="MTFCC10">XXXXX</SimpleData>
    <SimpleData name="FUNCSTAT10">X</SimpleData>
    <SimpleData name="ZIPNUMERIC">XXXXX</SimpleData>
    <SimpleData name="ALAND10">XXXX</SimpleData>
    <SimpleData name="AWATER10">XXXX</SimpleData>
    <SimpleData name="INTPTLAT10">XX.XXXX</SimpleData>
    <SimpleData name="INTPTLON10">XX.XXXX</SimpleData>
    <SimpleData name="ZIPcode">XXXXX</SimpleData>
    <SimpleData name="AreaName">XXXXX</SimpleData>
    <SimpleData name="TaxYear">XXXXX</SimpleData>
    <SimpleData name="numreturns">XXX</SimpleData>
    <SimpleData name="numjointre">XXX</SimpleData>
    <SimpleData name="avg_agi">XXX</SimpleData>
</SchemaData></ExtendedData>
  <Polygon><outerBoundaryIs><LinearRing><coordinates>[lots of lat/lng data here removed for everyone's own convenience]</coordinates></LinearRing></outerBoundaryIs></Polygon>
</Placemark>

So if I wanted to access the SimpleData Value where the name = "ZIPcode" - is there a way I can do that? I have MANY <Placemark>'s in this document, so I can't use xpath, though I'm not even sure how I'd use Xpath based on an attribute. Maybe that's an option?

Edit

I'd love something like:

if(isset($xml->Document->Folder->Placemark)){
    foreach($xml->Document->Folder->Placemark as $place) {
        $zip = $place->xpath('//SchemaData/SimpleData[@name="ZIPcode"]/text()');
    }
}

The desired effect being that it pulls the value of the SimpleData element with the attribute of "ZIPcode", and it would do this for each Placemark element. But I can't get this code to return anything.

There's a little bit of the structure that was left out of the XML, but I left the necessary parts. If I left everything in, this would be a far larger document.

هل كانت مفيدة؟

المحلول

I don't get why you think this is not possible with XPath. In fact, it is one of the most basic use cases for XPath:

Placemark/ExtendedData/SchemaData/SimpleData[@name="ZIPcode"]/text()

نصائح أخرى

(Posted on behalf of the question author).

I found the true issue. If your KML or XML has a default namespace, you must define that namespace with registerXPathNamespace("c","namespace here") and use it in your xpath calls such as $xml->xpath('//c:element').

I was not receiving any error messages, just retrieving an empty array each time.

I know is a bit extreme, but why you don't grep or find the line that contains <SimpleData name="ZIPcode">

and on this line use substring or regex?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top