Extending an xpath request - how to do that?

well i am learing - and this code snippets make me learn alot.

I need your help here

The following code lists all schools and tries to obtain their names as
well. I have not covered translations yet because my sample data didn’t
have those, but you can also look for all kind of names including
translations and just prefer a specific one):

// 2.) Work with the XML Result

# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
    # Get the name of the school (if any), again with xpath
    list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
    printf("#%02d: ID:%' -10s  [%s,%s]  %s\n", $index, $school['id'],
$school['lat'], $school['lon'], $name);

The key point here are the xpath queries.

In the above mentioend example two are used, the first xpath queriy is
to get the nodes that have certain tags.I think this is the most
interesting one for me:

//node[tag[@k = "amenity" and @v = "school"]]

This line says: Give us all node elements that have a tag element inside
which has the k attribute value “amenity” and the v attribute value
“school”. This is the condition we have to filter out those nodes that
are tagged with amenity school.

Further on xpath is used again, now relative to those school nodes to
see if there is a name and if so to fetch it:

tag[@k = "name"]/@v'

This line says: Relative to the current node, give me the v attribute
from a tag element that as the k attribute value “name”. As you can see,
some parts are again similar to the line before. I think you can both
adopt them to your needs.

Because not all school nodes have a name, a default string is provided
for display purposes by adding it to the (then empty) result array:

list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
                                                Provide Default Value

So here my results for that code-example:

and now i try to figure out how i can enter more xpath queries at the
above mentioned code

and get out even more important data - see here



i will digg into all documents and come back later the weekend… and
report all the findings

well - i think that i need to extend the xpath requests within the loop
where xpath is used again,
now relative to those school nodes to see if there is a name and if so
to fetch it:

tag[@k = "name"]/@v' tag[@k = "contact:website"]/@v' tag[@k = "contact:email"]/@v'

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs