Games with XPath



    XML

    XML is a text format designed to store structured data (instead of existing database files ), to exchange information between programs , and to create more specialized markup languages ​​(for example, XHTML ), sometimes called dictionaries, on its basis . XML is a simplified subset of SGML .

    XML is a convenient thing to keep files readable.

    For example, a simple XML file might be

    1Vasilii182Anton203Petro35

    This file will show how to work with XPath.

    As you can see from the example, XML is convenient in that it is easy to read, structured. It is convenient to store data in this format; 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 Vasily branch.

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

    What should I do?

    The solution to this problem is XPath .

    XPath is a kind of language, as it were said, a language for querying an XML document that returns an iterator to branches satisfying conditions.

    So there is a document.

    We will write the PHP code for this document. This example uses SimpleXML functions 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, access can be done as: $ xml-> data-> row [0] -> name == Valilii.

    Now actually XPath.

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

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

    Key points when using:

    / - root node

    // - many nodes satisfying the following condition (more detailed on the wiki)

    * - any characters

    @ - attribute

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

    and, or - AND, OR.

    More details are written at ru.wikipedia.org/wiki/XPath.

    We will select all the 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 make 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)
    


    We make a selection for all with 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 just writing row [@ company = ..] is impossible.

    Let's make a sample of people who are over 18 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 that combining is very simple.
    The following example will show how you can use the built-in functions, for example
    last () - we 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)
    


    Now, let's find people who DO NOT work for 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. It performs basic functions.
    For more complex ones, there is XQuery, but this is not about that.

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

    Thanks.
    To be continued.


    Primary source

    Also popular now: