0

I am not so failiar with XML handling/managing, so I would like to ask your help in the following issue: I have an XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="somethiong_is_here...">
    <node1>value1</node1>
    <node2>
        <node21>
            <node211 enabled="true">
                <node2111>valuvavalue</node2111>
            </node211>
        </node21>
      </node2>
</root>

What I want, to change and/or replace the root node. It is fine for me, if I delete all attributes, namespace settings (all)...or create a new root node and add all children to the new node, where the result should be:

<?xml version="1.0" encoding="UTF-8"?>
    <root>
        <node1>value1</node1>
        <node2>
            <node21>
                <node211 enabled="true">
                    <node2111>valuvavalue</node2111>
                </node211 >
            </node21>
          </node2>
    </root>

As I mentioned, I am not so familiar with PHP - XML working. I tried it with SimpleXMLElement, DOMDocument, DOMElement, etc... But I wasn't able to delete root attributes or fully replace the root node.

I am using Php7.3

Thanks, Attila

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
aBanhidy
  • 257
  • 1
  • 3
  • 11
  • Why don't you create a new document? What is your goal? I don't see a relevant difference between two versions. – Markus Zeller Aug 19 '22 at 10:10
  • The main goal would be to remove/delete the namesapce settigns from the root node, because all the xml content what I got, created by users - not generated - and unfortunately they make syntax mistakes. With the result XML I would be able to get values with xPath...but if the namespace setting has syntax error, the xPath not work :( – aBanhidy Aug 19 '22 at 10:16
  • Does this answer your question? [Remove namespace from XML using PHP](https://stackoverflow.com/questions/1245902/remove-namespace-from-xml-using-php) – Markus Zeller Aug 19 '22 at 10:32
  • Unfortunaltey not :( – aBanhidy Aug 19 '22 at 12:15
  • Here is no namespace definition affecting the elements in your example. Removing a namespace that is used by elements/attributes requires to completely recreate all nodes without namespaces, otherwise the namespace on the node will add the required namespace definition. – ThW Aug 27 '22 at 15:29

1 Answers1

1

If you have the XML as a string

$xmlString = <<<'_XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="somethiong_is_here...">
    <node1>value1</node1>
    <node2>
        <node21>
            <node211 enabled="true">
                <node2111>valuvavalue</node2111>
            </node211>
        </node21>
      </node2>
</root>
_XML;

you could remove all text within the root tag.

$noNamespace = preg_replace('/<root[^>]*>/', '<root>', $xmlString);

Then load the cleaned up XML as a SimpleXML object and process as used.

$xml = simplexml_load_string($noNamespace);
print_r($xml);

and looks like

SimpleXMLElement Object
(
    [node1] => value1
    [node2] => SimpleXMLElement Object
        (
            [node21] => SimpleXMLElement Object
                (
                    [node211] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [enabled] => true
                                )

                            [node2111] => valuvavalue
                        )

                )

        )

)
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35