Solved

XML XPath Query on multiple attributes in PHP

Posted on 2010-09-21
7
1,719 Views
Last Modified: 2012-05-10
I have the following XML file..

<?xml version="1.0"?>
<events>
  <tag user="168" show="shows.xml" touch="1285133609" name="special_event" created="1285131987"/>
  <tag user="168" show="media.xml" touch="1285135678" name="special_event" created="1285132134"/>
</events>

Open in new window


I can use xpath like this to grab an attribute fine... count($results) = 2

$results = $xml->xpath('/events/tag[@user=168]');

Open in new window


but I want to grab two attributes, at the same time and look for an exact match on both cases
i thought i could do it like this but doesn't work?

$results = $xml->xpath('/events/tag[@user=168] and /events/tag[@show=show.xml]');

Open in new window


Anyone got an idea where I've gone wrong?

Thx
0
Comment
Question by:edjent
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 33732148
Try:
$results = $xml->xpath('/events/tag[@user=168 and @show=show.xml]');

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 33732151
additional it should be @show=shows.xml for the given xml
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 33732215
small note on hernst42's suggestion

> $results = $xml->xpath('/events/tag[@user=168 and @show=show.xml]');

That is exactly the way you could do this
BUT
@show = show.xml
will look for the element <show.xml> to be equal to the content of the attribute
you mean a string, so you will need quotes

$results = $xml->xpath('/events/tag[@user=168 and @show="shows.xml"]');

I usually do this, doesn't make much of a difference

$results = $xml->xpath('/events/tag[@user=168][@show="shows.xml"]');
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33733443
SimpleXML can give you an array of objects.  This makes it VERY easy to use an iterator.  Here is how I solve the problem.  Best to all, ~Ray
<?php // RAY_xml_example_243.php

error_reporting(E_ALL);



// TEST DATA FROM THE POST AT EE MODIFIED TO INCLUDE ONE DUPLICATED ATTRIBUTE COMBINATION

$xml = <<<EOXML

<?xml version="1.0"?>

<events>

  <tag user="168" show="shows.xml" touch="1285133609" name="special_event" created="1285131987"/>

  <tag user="168" show="books.xml" touch="1285135678" name="special_event" created="1285132134"/>

  <tag user="168" show="media.xml" touch="1285135678" name="special_event" created="1285132134"/>

  <tag user="168" show="books.xml" touch="1285135678" name="special_event" created="1285132134"/>

</events>

EOXML;



// MAKE AN OBJECT

$obj = SimpleXML_Load_String($xml);



// VISUALIZE THE OBJECT

echo "<pre>" .  PHP_EOL;

var_dump($obj);



// USE AN ITERATOR TO LOOK A THE OBJECT

$arr = array();

foreach ($obj as $key => $tag)

{

    // EXTRACT ATTRIBUTES FROM THE TAGS

    $u = $tag["user"];

    $s = $tag["show"];

    echo PHP_EOL . "$u $s";



    // IDENTIFY REPLICATED ATTRIBUTE COMBINATIONS

    if (in_array($u.$s, $arr)) echo " DUPLICATE";

    $arr[] = $u.$s;

}

Open in new window

0
 
LVL 1

Author Comment

by:edjent
ID: 33741363
I really didn't want to iterate through the values as this sheet will end up being really long over time. xpath seems much quicker option.

I used the expressions above in researching but I wasn't able to get the 'and' operator to work correctly or by stacking the vars [@1][@2], least so it appeared.

Gertone nailed it show.xml is a string, to be compared. I failed purely without the "" as it wouldn't match! pure late night oversight!

doh! thanks guys
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33741432
welcome
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33742878
Regarding this" "I really didn't want to iterate through the values as this sheet will end up being really long over time. xpath seems much quicker option."

You might want to test the relative speed of the processes.  How do you think Xpath finds things?  It's not magic, and it does not know where they are in advance.  It uses its own iterators and comparisons.  It may take millions of iterations before you see a second or two of difference between the coding methods.  Nothing wrong with using Xpath, but I find there is more control in the iterators.  It's just my preference.

 When the only tool you have is a hammer, everything looks like a nail ;-)

Good luck with your project, ~Ray
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
C# SQL BULK INSERT CLASS 5 36
Test ddwrt:UserLookup 1 24
Getting info from database not working 6 19
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now