PHP code example of bluem / xmltransformer

1. Go to this page and download the library: Download bluem/xmltransformer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bluem / xmltransformer example snippets


return [
    XMLTransformer::RULE_TAG => 'demo',
    '@xml:id' => 'id',
    '@foo' => false,
    XMLTransformer::RULE_ADD_AFTER => '!',
];



use BlueM\XMLTransformer;

echo XMLTransformer::transformString(
    '<root><element>Hello world</element></root>',
    function($tag, $attributes, $opening) {
        return [
            XMLTransformer::RULE_TAG => false, // <-- Removes tag, but keeps content
        ];
    }
);
// Result: “Hello World”.

use BlueM\XMLTransformer;

function transform($tag, $attributes, $opening) {
    if ('hello-world' == $tag) {
        if (isset($attributes['xml:lang']) and
            'de' == $attributes['xml:lang']) {
            $str = 'Hallo Welt';
        } else {
            $str = 'Hello world';
        }
        return [
            XMLTransformer::RULE_TAG => false, // <-- Remove the tag, keep content
            XMLTransformer::RULE_ADD_BEFORE => $str,  // <- Insert literal content
        ];
    }

    if ('root' == $tag) {
        // We do not want the enclosing <root> tags in the output
        return [XMLTransformer::RULE_TAG => false];
    }
}

echo XMLTransformer::transformString(
    '<root><hello-world xml:lang="de" /></root>',
    'transform'
);
// Result: “Hallo Welt”

echo XMLTransformer::transformString(
    '<root><hello-world xml:lang="en" /></root>',
    'transform'
);
// Result: “Hello world”

echo XMLTransformer::transformString(
    '<root><remove>Hello </remove>World</root>',
        function($tag, $attributes, $opening) {
            switch ($tag) {
                case 'remove':
                    return false; // <-- Removes tag incl. content
                case 'root':
                case 'keep':
                    return [XMLTransformer::RULE_TAG => false]; // <-- Remove tag, keep content
                    break;
                default:
                    // Returning null is not necessary, as this
                    // is the default behaviour. It is equivalent
                    // to "Do not change anything."
                    return null;
            }
        }
);
// Result: “World”

echo XMLTransformer::transformString(
    '<root abc="def"></root>',
    function($tag, $attributes, $opening) {
        return [
            '@abc' => 'xyz'
        ];
    }
);
// Result: “<root abc="xyz"></root>”
// Please note that empty tags will always be returned with
// a space before the slash.

echo XMLTransformer::transformString(
    '<root xml:id="abc"><bla xml:id="def" blah="yes"/></root>',
    function($tag, $attributes, $opening) {
        return [
            '@foo' => 'bar', // Add attribute "foo" with value "bar"
            '@blah' => false, // Remove attribute "blah"
            '@xml:id' => '@id', // Rename attribute "xml:id" to "id"
        ];
    }
);
// Result: “<root id="abc" foo="bar"><bla id="def" foo="bar" /></root>”
// Please note that empty tags will always be returned with
// a space before the slash.

echo XMLTransformer::transformString(
    '<root xml:a="a" xml:b="b" id="foo">Content</root>',
    function($tag, &$attributes, $opening) {
        foreach ($attributes as $name => $value) {
            if ('xml:' === substr($name, 0, 4)) {
                unset($attributes[$name]); // Drop attributes in "xml" namespace
            }
        }
    }
);
// Result: “<root id="foo">Content</root>”