2017 © Pedro Peláez
 

library xml-fu

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

image

ikr/xml-fu

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

  • Thursday, September 17, 2015
  • by ikr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 435 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

Build Status, (*1)

About

Handy utility functions to query XML. Was initially written to simplify dealing with raw SOAP responses, but can also be useful in general, enabling a more functinal style of extracting data from XML., (*2)

Installation

Requires PHP 5.3+ and Composer., (*3)

composer require ikr/xml-fu:~1.1

API

Core

XmlFu\value($xml, $xpath, $default)

Searches the $xml string for the first node matching the given $xpath, and returns its string value. If the node wasn't found, the $default value is returned., (*4)

XmlFu\firstNode($xml, $xpath)

Returns a SimpleXMLElement instance for the first node matching the given $xpath in the given $xml string., (*5)

XmlFu\chunks($xml, $xpath)

Finds all the nodes in the passed $xml string matching the given $xpath, and returns an array of their asXML() serializations -- strings., (*6)

XmlFu\attrValues($xml, $elementXpath, $attrName)

Finds all the elements in the passed $xml string matching the given $elementXpath, and returns an array of their $attrName attribute values -- one string value per element., (*7)

Core without parsing

There are *Impl versions of the core functions, accepting $rootElement-s -- SimpleXMLElement instances instead of the $xml strings., (*8)

XmlFu\valueImpl($rootElement, $xpath, $default)
XmlFu\firstNodeImpl($rootElement, $xpath)
XmlFu\chunksImpl($rootElement, $xpath)
XmlFu\attrValuesImpl($rootElement, $elementXpath, $attrName)

That way you can avoid unnecessary multiple parsing runs when querying for multiple values., (*9)

Dealing with the default namespace

SimpleXMLElement::xpath() have some quirks dealing with the default XML namespace declaration: the xmlns="..." attribute. As soon as it's present, all the nodes contained by the element with xmlns declared, and the declaring element itself must be prefixed with a namespace in XPath queries. XmlFu conveniently aliases the default namespace with underscore -- _, (*10)

Thus,, (*11)

$xml = <<<XML
    <OTA_PingRS xmlns="http://www.opentravel.org/OTA/2003/05">
        <Success />
        <EchoData>Hey!</EchoData>
    </OTA_PingRS>    
XML;

echo XmlFu\value($xml, '//_:EchoData', null); // 'Hey!'

Extras

XmlFu\removeXmlProcessingInstruction($xml)

Removes the <?xml ... declaration from the passed $xml string. The removal is done in a safe way: no reg-exps, honest parsing., (*12)

XmlFu\rootTagName($xml)

The name of the topmost XML element in the passed $xml string., (*13)

Development status

Used in production., (*14)

The Versions

17/09 2015

dev-master

9999999-dev

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ivan Krechetov

19/08 2014

v1.1.0

1.1.0.0

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ivan Krechetov

29/04 2014

v1.0.0

1.0.0.0

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

  Sources   Download

MIT

The Development Requires

by Ivan Krechetov