PHPackages                             ikr/xml-fu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ikr/xml-fu

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ikr/xml-fu
==========

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

v1.1.0(11y ago)0593MITPHPPHP &gt;=5.3.0

Since Apr 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/ikr/xml-fu)[ Packagist](https://packagist.org/packages/ikr/xml-fu)[ RSS](/packages/ikr-xml-fu/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

[![Build Status](https://camo.githubusercontent.com/451f451db6cd9fad2722b0b919204adb0d75363bc05d5e038a1ce27df8a1305d/68747470733a2f2f7472617669732d63692e6f72672f696b722f786d6c2d66752e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ikr/xml-fu)

About
=====

[](#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.

Installation
============

[](#installation)

Requires PHP 5.3+ and [Composer](https://getcomposer.org/).

```
composer require ikr/xml-fu:~1.1

```

API
===

[](#api)

Core
----

[](#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.

```
XmlFu\firstNode($xml, $xpath)

```

Returns a `SimpleXMLElement` instance for the first node matching the given `$xpath` in the given `$xml` string.

```
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.

```
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.

Core without parsing
--------------------

[](#core-without-parsing)

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

```
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.

Dealing with the default namespace
----------------------------------

[](#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 -- `_`

Thus,

```
$xml =
