PHP xpath() Function

In this chapter you will learn:

  1. Description for PHP xpath() Function
  2. Syntax for PHP xpath() Function
  3. Parameter for PHP xpath() Function
  4. Return for PHP xpath() Function
  5. Example - runs an XPath query on the XML document

Description

The xpath() function runs an XPath query on the XML document.

Syntax


  class SimpleXMLElement{
   string xpath(path)
}

Parameter

ParameterIs requiredDescription
pathRequired.What to search for in the XML document

Return

This function returns an array of SimpleXMLElements on success, and FALSE of failure.

Example


<?xml version="1.0" encoding="ISO-8859-1"?>
<book>//from j  av a2 s  .com
    <name>PHP</name>
    <name>Java</name>
</book>

PHP Code


<?php//from   j  a  v  a  2 s . co m
    $xml = simplexml_load_file("test.xml");
    
    $result = $xml->xpath("name");
    
    print_r($result);
?>

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP addcslashes() Function
  2. Syntax for PHP addcslashes() Function
  3. Parameter for PHP addcslashes() Function
  4. Return for PHP addcslashes() Function
  5. Example - Add a backslash in front of the character "W"
  6. Example - Add backslashes to certain characters in a string
  7. Example - Add backslashes to a range of characters in a string
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function