Asked  7 Months ago    Answers:  5   Viewed   41 times

I am performing a search in an XML file, using the following code:

$result = $xml->xpath("//StopPoint[contains(StopName, '$query')]");

Where $query is the search query, and StopName is the name of a bus stop. The problem is, it's case sensitive.

And not only that, I would also be able to search with non-english characters like ÆØÅæøå to return Norwegian names.

How is this possible?

 Answers

88

In XPath 1.0 (which is, I believe, the best you can get with PHP SimpleXML), you'd have to use the translate() function to produce all-lowercase output from mixed-case input.

For convenience, I would wrap it in a function like this:

function findStopPointByName($xml, $query) {
  $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ"; // add any characters...
  $lower = "abcdefghijklmnopqrstuvwxyzæøå"; // ...that are missing

  $arg_stopname = "translate(StopName, '$upper', '$lower')";
  $arg_query    = "translate('$query', '$upper', '$lower')";

  return $xml->xpath("//StopPoint[contains($arg_stopname, $arg_query)");
}

As a sanitizing measure I would either completely forbid or escape single quotes in $query, because they will break your XPath string if they are ignored.

Wednesday, March 31, 2021
 
linjuming
answered 7 Months ago
79

Hi had a simillar problem with a long in script, I have taken the idea from there and came up with this i hope it helps

<?php

$xml = simplexml_load_file("photo.xml");

for ($i = 0; $i < count($xml); $i++){

if(isset($_GET["LocationName"]))
{
    $photographer_id = $_GET["LocationName"];
}

$result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"]  ');
}


if(isset($_GET["photographer"]))
{
    $photographer = $_GET["photographer"];
} 

if(isset($_GET["images"]))
{
    $image = $_GET["images"];

    //$result = $xml->xpath('/root/area[photographer_id="' . $photographer_id . '"] and /root/area[photographer="' . $photographer . '"]');

}

//echo $photographer_id;
//echo $photographer;
//echo $image;

$filteredResult = array();

foreach($result as $obj){
    if(in_array($photographer, (array)$obj) == 1 || $photographer == 'x'){ 
        if(in_array($image, (array)$obj) || $image == 'x'){
            array_push($filteredResult, $obj);
        }
    }
}

foreach($filteredResult as &$obj){
    //how to access values in the object
    echo $obj->{'photographer'};
    echo $obj->{'image'};
}
echo $obj->{'image'};

    ?>

the last echo should give you some pointers

Wednesday, March 31, 2021
 
Owen
answered 7 Months ago
38

You could use the type to group periods and fetch the first of the grouped results:

$xpath->evaluate(
    'string(
        abc:Entity/abc:Periods/abc:Period[
            abc:Type = "ACCOUNTING" or abc:Type="BALANCED"
        ][1]/abc:Type
    )', 
    $node
),

Or you check if here is an abc:End child element:

$xpath->evaluate(
    'string(
        abc:Entity/abc:Periods/abc:Period[
            count(abc:End) = 0
        ][1]/abc:Type
    )', 
    $node
),
Wednesday, March 31, 2021
 
Alix
answered 7 Months ago
10

Turns out the tool does not support XPath 2.0.

XPath 1.0 doesn't have the fancy min() and max() functions, so to find these values we need to be a little tricky with the XPath logic, and compare the values on the siblings of the node:

Maximum:

/foo/bar[not(preceding-sibling::bar/@score >= @score) 
    and not(following-sibling::bar/@score > @score)]/@score

Minimum:

/foo/bar[not(preceding-sibling::bar/@score <= @score) 
    and not(following-sibling::bar/@score < @score)]/@score

If embedding these queries in XML files like XSLT or ant scripts, remember to encode < and > as &lt; respecting &gt;.

Thursday, June 3, 2021
 
sunshinejr
answered 5 Months ago
94
grep -nr 'yourString*' .

The dot at the end searches the current directory. Meaning for each parameter:

-n            Show relative line number in the file
'yourString*' String for search, followed by a wildcard character
-r            Recursively search subdirectories listed
.             Directory for search (current directory)

grep -nr 'MobileAppSer*' . (Would find MobileAppServlet.java or MobileAppServlet.class or MobileAppServlet.txt; 'MobileAppASer*.*' is another way to do the same thing.)

To check more parameters use man grep command.

Saturday, June 26, 2021
 
uiroshan
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :