Comments (7)
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.
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.
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.
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.
@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.
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.
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)
- Feature: decode_from_element()
- Feature: element_encode() HOT 1
- Fix PSL 2 support
- Use of mutation testing in xml - Help needed HOT 1
- xmlns:default added, how to remove? HOT 3
- [RFC] Provide matching sequence on XML\Reader HOT 2
- Improve functions autloader
- Roadmap HOT 2
- forFile fails if file does not exist HOT 2
- Investigate spec compliance (PHP 84)
- Loading UTF-16 XML HOT 3
- Fatal error: Cannot redeclare VeeWee\Xml\Dom\Assert\assert_attribute() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xml.