📜 ⬆️ ⬇️

XPath Games



XML

XML is a text format intended for storing structured data (instead of existing database files ), for exchanging information between programs , as well as for creating more specialized markup languages ​​(for example, XHTML ), sometimes called dictionaries . XML is a simplified subset of SGML .
')
XML is a handy thing to store files in a readable form.

For example, a simple XML file might be

  <? xml version = "1.0" encoding = "UTF-8"?>
 <root>

  <data>

     <row>
          <id> 1 </ id>
          <name company = "ibm" status = "banned"> Vasilii </ name>
          <age> 18 </ age>

     </ row>

     <row>
           <id> 2 </ id>
           <name company = "ibm"> Anton </ name>
           <age> 20 </ age>
     </ row>
     <row>
           <id> 3 </ id>
           <name company = "apple"> Petro </ name>
           <age> 35 </ age>
      </ row>
  </ data>

 </ root> 

This file will show how to work with XPath.

As you can see from the example, XML is convenient because it is easy to read, structured. In this format, it is convenient to store data, moreover, there are many libraries for working with this type of file.

Any element has its own path, for example, the root-data-row [0] path will point to the basil branch.

But when a programmer starts using this file, he has problems with file manipulation. For example, you must select all people whose age is <20.

What to do?

The solution to this problem is XPath .

XPath is a kind of language, so I said, a query language to an XML document that returns an iterator to the branches that satisfy the conditions.

So, there is a document.

Write the PHP code for this document. In this example, SimpleXML functions are used to work.

There is a code:
  $ xml = simplexml_load_file ("db.xml");
 var_dump ($ xml); 

The result will be as follows:
  object (SimpleXMLElement) [1]
   public 'data' =>
     object (SimpleXMLElement) [2]
       public 'row' =>
         array
           0 =>
             object (SimpleXMLElement) [3]
               public 'id' => string '1' (length = 1)
               public 'name' => string 'Vasilii' (length = 7)
               public 'age' => string '18' (length = 2)
           1 =>
             object (SimpleXMLElement) [4]
               public 'id' => string '2' (length = 1)
               public 'name' => string 'Anton' (length = 5)
               public 'age' => string '20' (length = 2)
           2 =>
             object (SimpleXMLElement) [5]
               public 'id' => string '3' (length = 1)
               public 'name' => string 'Petro' (length = 5)
               public 'age' => string '35' (length = 2) 

As you can see from the result, the access can be done as: $ xml-> data-> row [0] -> name == Valilii.

Now XPath proper.

XPath allows you to use conditions and its built-in functions for fetching branches.

For example / (slash) - indicates the hierarchy of branches, for example / data,

Key points for use:

/ - root node

// - the set of nodes satisfying the trace condition (more on the wiki)

* - any characters

@ - attribute

[] - analogue () in sql, sets the conditions

and, or - AND, OR.

Written in more detail.wikipedia.org/wiki/XPath

Make a selection of all elements using XPath.

  foreach ($ xml-> xpath ("// row") as $ res) var_dump ($ res); 

  object (SimpleXMLElement) [6]
   public 'id' => string '1' (length = 1)
   public 'name' => string 'Vasilii' (length = 7)
   public 'age' => string '18' (length = 2)

 object (SimpleXMLElement) [7]
   public 'id' => string '2' (length = 1)
   public 'name' => string 'Anton' (length = 5)
   public 'age' => string '20' (length = 2)

 object (SimpleXMLElement) [8]
   public 'id' => string '3' (length = 1)
   public 'name' => string 'Petro' (length = 5)
   public 'age' => string '35' (length = 2)


Let's do a sample where age> 18
  foreach ($ xml-> xpath ("// row [age> 18]") as $ res) var_dump ($ res); 

Result:
  object (SimpleXMLElement) [6]
   public 'id' => string '2' (length = 1)
   public 'name' => string 'Anton' (length = 5)
   public 'age' => string '20' (length = 2)

 object (SimpleXMLElement) [7]
   public 'id' => string '3' (length = 1)
   public 'name' => string 'Petro' (length = 5)
   public 'age' => string '35' (length = 2)


Make a selection of all who have the attribute company = 'ibm'
  foreach ($ xml-> xpath ("// row [name [@ company = 'ibm']]") as $ res) var_dump ($ res); 

Result:
  object (SimpleXMLElement) [6]
   public 'id' => string '1' (length = 1)
   public 'name' => string 'Vasilii' (length = 7)
   public 'age' => string '18' (length = 2)

 object (SimpleXMLElement) [7]
   public 'id' => string '2' (length = 1)
   public 'name' => string 'Anton' (length = 5)
   public 'age' => string '20' (length = 2)

It should be noted that the @company attribute is part of the name, so simply writing a row [@ company = ..] is impossible.

Let's make a sample of people who are over 18 years old and work at IBM
  foreach ($ xml-> xpath ("// row [name [@ company = 'ibm'] and age> 18]") as $ res) var_dump ($ res); 

Result:
  object (SimpleXMLElement) [6]
   public 'id' => string '2' (length = 1)
   public 'name' => string 'Anton' (length = 5)
   public 'age' => string '20' (length = 2) 


You can see what to combine is very simple.
The following example will show how you can use built-in functions, for example
last () - get the last person in the list.
  foreach ($ xml-> xpath ("// row [last ()]") as $ res) var_dump ($ res); 

Result:
  object (SimpleXMLElement) [6]
   public 'id' => string '3' (length = 1)
   public 'name' => string 'Petro' (length = 5)
   public 'age' => string '35' (length = 2)


And now we find people who do NOT work at IBM:
  foreach ($ xml-> xpath ("// row [name [not (@ company = 'ibm')]]") as $ res) var_dump ($ res); 

Result:
  object (SimpleXMLElement) [6]
   public 'id' => string '3' (length = 1)
   public 'name' => string 'Petro' (length = 5)
   public 'age' => string '35' (length = 2)


Everything is very simple and convenient!

results


Xpath is a good tool for working with an XML document. Basic functions are performed.
For more complex ones, there is XQuery, but this is not the case now.

XML + XPath is a worthy replacement in those places where it is inconvenient to use a database for some reason.

Thank.
To be continued.


Original source

Source: https://habr.com/ru/post/65383/


All Articles