Git Product home page Git Product logo

Comments (7)

veewee avatar veewee commented on June 15, 2024

Hello,

Thanks for reporting. Would you mind adding a code example?
It is not completely clear what you are trying to do, how you do it, what is going wrong exactly, what you expect, ...

from xml.

rauanmayemir avatar rauanmayemir commented on June 15, 2024

I've already deleted the traversal and went with xpath query and calling remove directly there on the node list:

$doc = Document::fromXmlString($xml);
$doc
    ->xpath()
    ->query('*//*[not(node())]')
    ->forEach(fn(DOMNode $node) => remove($node));

But I can see that there's no test for deep nested traversal and/or remove node action: https://github.com/veewee/xml/blob/main/tests/Xml/Dom/Traverser/TraverserTest.php.

Simply adjusting the node's attributes, tag or contents works fine as it's just traversing and mutating the objects, but returning remove node action will only work once.

What I was trying to do is remove all empty nodes in the xml like <nodeTag xsi:nil="true"/> or even just <nodeTag />.

from xml.

veewee avatar veewee commented on June 15, 2024

I'm still confused to be honest.
Wouldn't you be able to perform the removal at onNodeLeave ?
That way you delete from the deepest level back to the -> root
We do something similar whilst removing namespaces here: https://github.com/veewee/xml/blob/main/src/Xml/Dom/Traverser/Visitor/RemoveNamespaces.php

The code you pasted is not recursive either.
We've implemented this in another package as well:
https://github.com/php-soap/psr18-transport/blob/main/src/Middleware/RemoveEmptyNodesMiddleware.php

As you can see, the xpath should go in a loop.
Yet it would be better if you'dd remove with the traverser instead.

from xml.

rauanmayemir avatar rauanmayemir commented on June 15, 2024

The code you pasted is not recursive either.

Yes, but via xpath I'm just pulling all the empty nodes and removing them. It actually worked for me. 🙈

We've implemented this in another package as well:

Thank god, I was looking for this in the wrong place.

We do something similar whilst removing namespaces here:

Yes, this is mutating the node sort of in-place. But if you were to replace it with return new Action\RemoveNode(), it wouldn't work as expected.

from xml.

veewee avatar veewee commented on June 15, 2024

@rauanmayemir Thanks for reporting! I found the issue in #50 !
Can you check if that works for you? I've added a visitor as well, so that you dont have to rewrite the code.
The problem was quite tricky :)

from xml.

rauanmayemir avatar rauanmayemir commented on June 15, 2024

Can confirm now it works as expected. 💃

I would still rather use RemoveEmptyNodesMiddleware as a preflight clean up, but I'm glad I can finally use this package for safe general XML manipulation. (I'm already using the writer for smooth xml generation, but had trouble starting with the traversal, locators and manipulation - they need more examples. 😁 )

from xml.

veewee avatar veewee commented on June 15, 2024

Nice to hear!
I can imagine it takes some getting-to-know-the-package, but feel free to provide better examples in the documentation :)

from xml.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.