2017 © Pedro Peláez
 

library xml-transform

XML to Array transformer

image

cdekok/xml-transform

XML to Array transformer

  • Monday, January 2, 2017
  • by mech7
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,233 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 11 % Grown

The README.md

Build Status Coverage Status Total Downloads License SensioLabsInsight, (*1)

PHP XML Transformer

This library is useful to map xml values to array values, with xpath queries., (*2)

Installation

composer require cdekok/xml-transform, (*3)

Usage

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

For convience it's also possible to only map to 1 array instead of a list of results., (*4)

$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

$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 empty values from the returned array, (*5)

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

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, (*6)

$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

After running composer install grumphp will watch codestyles and unit tests before commits., (*7)

To manually check the code style / unit tests run composer run test, (*8)

To format the code automatically run composer run format, (*9)

To generate test coverage run composer run report, (*10)

This project follows git flow for commits, (*11)

The Versions

02/01 2017

dev-develop

dev-develop

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

02/01 2017

dev-master

9999999-dev

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

02/01 2017

1.1.3

1.1.3.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

02/01 2017

1.1.2

1.1.2.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

02/01 2017

1.1.1

1.1.1.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

28/11 2016

1.1.0

1.1.0.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

28/11 2016

1.0.1

1.0.1.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

1.0.0

1.0.0.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

0.2.4

0.2.4.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

0.2.3

0.2.3.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

0.2.2

0.2.2.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

0.2.1

0.2.1.0

XML to Array transformer

  Sources   Download

MIT

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

26/11 2016

0.2.0

0.2.0.0

XML to Array transformer

  Sources   Download

The Requires

  • php ^7.0
  • lib-libxml *

 

The Development Requires

by cdekok

xml mapping transformer

25/11 2016

0.1.1

0.1.1.0

XML to Array transformer

  Sources   Download

The Requires

  • php ^7.0

 

The Development Requires

by cdekok

xml mapping transformer

25/11 2016

0.1.0

0.1.0.0

XML to Array transformer

  Sources   Download

The Requires

  • php ^7.0

 

The Development Requires

by cdekok

xml mapping transformer