PHPackages                             fransverbeek/xml-transform - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. fransverbeek/xml-transform

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

fransverbeek/xml-transform
==========================

XML to Array transformer

2.0.0(3y ago)0562MITPHPPHP ^8.0CI failing

Since Feb 7Pushed 3y agoCompare

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

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

[![Build Status](https://camo.githubusercontent.com/2cdd51daffe649d4de575571390c607fcece269dcf5e62abe161535fef2cf7ce/68747470733a2f2f7472617669732d63692e6f72672f6364656b6f6b2f786d6c2d7472616e73666f726d2e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/cdekok/xml-transform)[![Coverage Status](https://camo.githubusercontent.com/92d5b3eaaad899026e999ca0cf53b4cbd76e108936a7365ce1038e3372a13642/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6364656b6f6b2f786d6c2d7472616e73666f726d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/cdekok/xml-transform?branch=master)[![Total Downloads](https://camo.githubusercontent.com/0fe76e28d3989d22b141e33c89d2a0c1823e4a19c0c07f71af23c4907c7e7c76/68747470733a2f2f706f7365722e707567782e6f72672f6364656b6f6b2f786d6c2d7472616e73666f726d2f646f776e6c6f616473)](https://packagist.org/packages/cdekok/xml-transform)[![License](https://camo.githubusercontent.com/e4e09638728fecc85a6af4916fc7fc0c402b1c1b757aebbcdcb12ab4657f8c6b/68747470733a2f2f706f7365722e707567782e6f72672f6364656b6f6b2f786d6c2d7472616e73666f726d2f6c6963656e7365)](https://packagist.org/packages/cdekok/xml-transform)[![SensioLabsInsight](https://camo.githubusercontent.com/82d795e1dd83b342fda10eb84eff8f8b90a1444bd903206fe8ef295f60dc9d76/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65333632643564382d316239382d346436632d613135332d3639303434353866366437372f6d696e692e706e67)](https://insight.sensiolabs.com/projects/e362d5d8-1b98-4d6c-a153-6904458f6d77)

PHP XML Transformer
===================

[](#php-xml-transformer)

This library is useful to map xml values to array values, with xpath queries.

Installation
------------

[](#installation)

`composer require cdekok/xml-transform`

Usage
-----

[](#usage)

### List of data

[](#list-of-data)

```
// Optional add namespaces in the XML
$namespaces = ['oai' => 'http://www.openarchives.org/OAI/2.0/'];

// Define the mapping for the array that you want to have filled
$mapping = [
    'id' => [
        'xpath' => './/oai:identifier/text()'
    ],
    'material' => [
        'xpath' => './/oai:material/text()',
        'repeatable' => true // If elements are repeatable set this option so an array will be returned
    ],
];

$data = (new \XmlTransform\Mapper($mapping, '//oai:OAI-PMH/oai:ListRecords/oai:record', $namespaces))
    ->from('somefile.xml')
    ->transform();

// $data will contain something like
[
    ['id' => '12', 'material' => ['paint', 'pencil']],
    ['id' => '13', 'material' => ['pen', 'pencil']],
]
```

### Single array

[](#single-array)

For convience it's also possible to only map to 1 array instead of a list of results.

```
$data = (new \XmlTransform\Mapper($mapping, '//oai:OAI-PMH/oai:ListRecords/oai:record', $namespaces))
    ->from('somefile.xml')
    ->transformOne();

// $data will contain something like
['id' => '12', 'material' => ['paint', 'pencil']]
```

### Repeatable nested elements

[](#repeatable-nested-elements)

```
$mapping = [
    'id' => ['xpath' => './/oai:objectid/text()'],
    'creator' => [
        'repeatable' => true, // Mark the element as repeatable
        'context' => './/oai:constituent', // new context for the nested elements
        'values' => [
            'name' => ['xpath' => './/text()'],
            'death_date' => ['xpath' => './/@death_date'],
        ]
    ]
];

$transformer = new \XmlTransform\Mapper($mapping, '//oai:record', $namespaces);
$result = $transformer->from($xml)->transformOne();

// Result will contain something like this
[
    'id' => '3517',
    'creator' => [
        ['name' => 'Rembrandt', 'death_date' => '1669'],
        ['name' => 'Johannes Mock', 'death_date' => '1884'],
        ['name' => 'Georg Friedrich Schmidt', 'death_date' => '1775'],
    ]
]
```

### Filter values

[](#filter-values)

Filter empty values from the returned array

```
$transformer->from($xml)->filter()->transform();
```

### Optional elements (contexts)

[](#optional-elements-contexts)

If there are optional elements with a context in the xml you will need to enable the optional setting to prevent an ContextNotFoundException

```
$mapping = [
    'record' => [
        'context' => './/data',
        'values' => [
            'title' => ['xpath' => './/title/text()'],
            'creator' => ['xpath' => './/creator/text()'], // optional
        ]
    ],
];

$transformer = new \XmlTransform\Mapper($mapping, '//record');
$result = $transformer->from($xml)->optionalElements()->filter()->transform();

// Result creator is missing.
[
    'record' => [
        'title' => 'test',
        'creator' => 'Bert',
    ],
],
[
    'record' => [
        'title' => 'test 2',
    ]
]
```

Development
-----------

[](#development)

After running `composer install` grumphp will watch codestyles and unit tests before commits.

To manually check the code style / unit tests run `composer run test`

To format the code automatically run `composer run format`

To generate test coverage run `composer run report`

This project follows [git flow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow) for commits

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1196d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3be43481cb12f49930367d6c11c576958fec71911859f7eee00ef470665dc3d3?d=identicon)[fransverbeek](/maintainers/fransverbeek)

---

Top Contributors

[![cdekok](https://avatars.githubusercontent.com/u/190633?v=4)](https://github.com/cdekok "cdekok (48 commits)")[![fransverbeek](https://avatars.githubusercontent.com/u/1070307?v=4)](https://github.com/fransverbeek "fransverbeek (5 commits)")

---

Tags

xmlmappingtransformer

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/fransverbeek-xml-transform/health.svg)

```
[![Health](https://phpackages.com/badges/fransverbeek-xml-transform/health.svg)](https://phpackages.com/packages/fransverbeek-xml-transform)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[sabre/xml

sabre/xml is an XML library that you may not hate.

52832.2M131](/packages/sabre-xml)[vipnytt/sitemapparser

XML Sitemap parser class compliant with the Sitemaps.org protocol.

772.2M10](/packages/vipnytt-sitemapparser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
