0

I have two similar nodes in an XML file. When using a CONTAINS query via XPATH, the XPATH query returns two sets of data. I would like it to only return the exact match. Any idea if this is possible?

Here is my code:

$uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lc = "abcdefghijklmnopqrstuvwxyz" ;
$lats = $users->xpath("/users/user[contains(translate(ID, '$uc', '$lc'), translate('$test', '$uc', '$lc'))]/lat");
$lons = $users->xpath("/users/user[contains(translate(ID, '$uc', '$lc') ,translate('$test', '$uc', '$lc'))]/lon");

Where $test = "NY:3036N-B"

XML file contains NY:3036N-B and NY:3036N-B2

Thanks in advance,

M

2
Contributors
6
Replies
27
Views
3 Years
Discussion Span
Last Post by Mapper99
0

Thanks...I gave it a try, but get this error:

Invalid expression

Old code:

$lats = $users->xpath("/users/user[contains(translate(ID, '$uc', '$lc'), translate('$test', '$uc', '$lc'))]/lat");

is now:

$lats = $users->xpath('/users/user[text(@ID==$test)]/lat');

Perhaps my syntax is incorrect?

Thanks,

M

Edited by Mapper99

0

Try:

$lats = $users->xpath('/users/user[text()=='$test']/lat');

or:

$lats = $users->xpath('/users/user[@ID=='$test']/lat');

Depends on the XML, which you didn't show.

0

Here is a snippet of the XML:

<users>
    <user>
        <ID>NY:3036N-B</ID>
        <lat>10</lat>
        <lon>20</lon>
    </user>
<users>  

I tried both examples. If I keep the code the same, I get this message:

Parse error: syntax error, unexpected '$test' (T_VARIABLE)

If I change the code (add double quotes), I get this error:

SimpleXMLElement::xpath(): Invalid expression

Any ideas?

Thanks,

Mark

Edited by Mapper99

1

Oh right, my bad, wrong quotes. I think it should be:

$lats = $users->xpath("/users/user[ID[text()='$test']]/lat");

or:

$lats = $users->xpath("/users/user/lat[../ID/text()='$test']");

Edited by pritaeas

0

$lats = $users->xpath("/users/user[ID[text()='$test']]/lat");

Works like a charm. Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.