2017 © Pedro Peláez
 

library xml-fuse

Small Library to turn merge data using XPaths

image

aydin-hassan/xml-fuse

Small Library to turn merge data using XPaths

  • Friday, July 31, 2015
  • by AydinHassan
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4,826 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 3 % Grown

The README.md

XmlFuse

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Untable Version, (*1)

A Small library for merging scalar data from multiple XPaths's. Similar to an SQL Join, but using XML., (*2)

This library is useful if you want to convert XML data into a flat format. This is useful for a source agnostic approach. For example, you may process data from multiple source but want to normalise them all to a similar format., (*3)

This library aims to convert 3 dimensional data in to a flat array. Take the following XML as an example:, (*4)

<?xml version="1.0" encoding="ISO-8859-1"?>
<order>
    <orderId>01</orderId>
    <customerEmail>aydin@hotmail.co.uk</customerEmail>
    <lines>
        <line>
            <id>1</id>
            <qty>2</qty>
            <status>despatched</status>
        </line>
        <line>
            <id>5</id>
            <qty>1</qty>
            <status>despatched</status>
        </line>
    </lines>
</order>

Imagine an import which updates the status of each item associated with an order. The import only accepts a flat array of data and can only process one item at a time. In order to update the status of an Item, the import must also know the order ID in order to load it and interact with its items., (*5)

We could use an xpath to loop each order item like so: //order/lines/line and this would give us all the item information, but we still don't have the order ID in the data. What if we could get the data like so:, (*6)

array(2) {
  [0] =>
  array(5) {
    'orderId' => string(2) "01"
    'customerEmail' => string(19) "aydin@hotmail.co.uk"
    'id' => string(1) "1"
    'qty' => string(1) "2"
    'status' => string(10) "despatched"
  }
  [1] =>
  array(5) {
    'orderId' => string(2) "01"
    'customerEmail' => string(19) "aydin@hotmail.co.uk"
    'id' => string(1) "5"
    'qty' => string(1) "1"
    'status' => string(10) "despatched"
  }
}

This data structure is each of the nodes within each <line> embedded with the parent node's data. We can succesfully pass each of these data structures to our order status updater class, and it has all the data it needs to do its work., (*7)

Installation

Composer

composer require aydin-hassan/xml-fuse

Usage

To use the tool you should instantiate the XmlFuser class, passing in your XML and the XPath's you want to combine. You can then call parse() to get an array of records back., (*8)

Example:

$xml = '
<order>
    <orderId>01</orderId>
    <customerEmail>aydin@hotmail.co.uk</customerEmail>
    <lines>
        <line>
            <id>1</id>
            <qty>2</qty>
            <status>despatched</status>
        </line>
        <line>
            <id>5</id>
            <qty>1</qty>
            <status>despatched</status>
        </line>
    </lines>
</order>';

$xPaths = [
    '//order',
    'lines/line'
];

$fuser = new \AydinHassan\XmlFuse\XmlFuse($xml, $xPaths);
$res = $fuser->parse();

var_dump($res);

//Output
array(2) {
  [0] =>
  array(5) {
    'orderId' => string(2) "01"
    'customerEmail' => string(19) "aydin@hotmail.co.uk"
    'id' => string(1) "1"
    'qty' => string(1) "2"
    'status' => string(10) "despatched"
  }
  [1] =>
  array(5) {
    'orderId' => string(2) "01"
    'customerEmail' => string(19) "aydin@hotmail.co.uk"
    'id' => string(1) "5"
    'qty' => string(1) "1"
    'status' => string(10) "despatched"
  }
}

What's going on?

Each of the XPath's passed in to the parser are processed in the order they are given. So the XML will be loaded and the first XPath search will be performed. This will produce an array of nodes (of which there is one)., (*9)

Each piece of data in the node, which isn't a nested structure will be moved to an array. So we have: orderId & customerEmail:, (*10)

[
   'orderId' => 1,
   'customerEmail' => 'aydin@otmail.co.uk',
]

is skipped because it is a nested structure., (*11)

Now from this point, the next XPATH is searched, remember our current position? (). Our second XPath is: lines/line this will return an array of nodes., (*12)

Now each nodes scalar data (non-nested) is converted to an array. Eg:, (*13)

[
   'id' => '1',
   'qty' => '1',
   'status' => 'despatched',
]

It is then merged with the data from the parent search, the node. Which will leave us with:, (*14)

[
   'orderId' => 1,
   'customerEmail' => 'aydin@otmail.co.uk',
   'id' => '1',
   'qty' => '1',
   'status' => 'despatched',
]

This is then repeated for each node., (*15)

Notes

  1. Any amount of XPaths can be given, if they return no results, then no merging will be done, the parent will be returned instead.
  2. You can use the setXPaths() method to pass in a new set of XPaths, and call the parse() method to reparse the XML.

Contributing & Running Tests

git clone git@github.com:AydinHassan/XmlFuse.git
cd XmlFuse
composer install
./vendor/bin/phpunit

//make sure you run the lint process aswell
./vendor/bin/phpcs --standard=PSR2 ./src
./vendor/bin/phpcs --standard=PSR2 ./test

The Versions

31/07 2015

dev-master

9999999-dev

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

31/07 2015

0.4.0

0.4.0.0

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

25/07 2014

0.3.0

0.3.0.0

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/07 2014

dev-nested-xml-parser

dev-nested-xml-parser

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

22/07 2014

0.2.0

0.2.0.0

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

06/07 2014

0.1.1

0.1.1.0

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

05/07 2014

0.1.0

0.1.0.0

Small Library to turn merge data using XPaths

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires