Comments (9)
Sounds quite reasonable to add, thanks for the request!
from rust-libxml.
Taking a look, the xmlTextWriter
API is completely separate from the xmlSave
API, so we can't really request indentation cleanly via the SetIndent
method, while still using xmlSaveToBuffer
.
I think the only viable approach is resorting to their global variable xmlIndentTreeOutput
, in combination with setting SaveOptions.format
to true. Using a global makes this a little iffy though, but worth taking another look...
from rust-libxml.
Actually, @matthew-nichols-westtel , could you offer an example/test where setting the format flag fails your expectations? A quick snippet from the tests is:
let doc_str_formatted = doc.to_string_with_options(SaveOptions {
format: true,
..SaveOptions::default()
});
Which comes out nicely indented over the unformatted.xml
test file. I have vague memories that there were a number of fine details in the way libxml handles spacing, but it has been way too long to remember them sharply.
from rust-libxml.
If you change the example to be slightly different it doesn't format entirely:
let parser = Parser { format: ParseFormat::XML };
let doc = parser.parse_string(r#"<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root"><h:table><h:tr><h:td>col 1</h:td><h:td>col 2</h:td></h:tr> </h:table>
<f:mock><f:doublemock /></f:mock><f:footer><h:table><h:tr><h:td>col 3</h:td><f:footer> nested f</f:footer></h:tr></h:table></f:footer></r:root>"#).unwrap();
println!("{}", doc.to_string_with_options(SaveOptions{format: true, no_declaration: true, ..Default::default()}));
Aka if there are existing spaces/newlines/etc it doesn't format as expected, this creates
<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root"><h:table><h:tr><h:td>col 1</h:td><h:td>col 2</h:td></h:tr> </h:table>
<f:mock><f:doublemock/></f:mock><f:footer><h:table><h:tr><h:td>col 3</h:td><f:footer> nested f</f:footer></h:tr></h:table></f:footer></r:root>
whereas xmllint --format
still creates
<?xml version="1.0"?>
<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root">
<h:table>
<h:tr>
<h:td>col 1</h:td>
<h:td>col 2</h:td>
</h:tr>
</h:table>
<f:mock>
<f:doublemock/>
</f:mock>
<f:footer>
<h:table>
<h:tr>
<h:td>col 3</h:td>
<f:footer> nested f</f:footer>
</h:tr>
</h:table>
</f:footer>
</r:root>
from rust-libxml.
Thanks for the example! Will give it another try soon.
from rust-libxml.
Alright, found out the little detail that makes xmllint --format
work. It sets xmlKeepBlanksDefault(0)
, immediately, which importantly has effect on parsing the input rather than serializing the output. Once the input spaces are discarded, the regular format serialization (that we already support in the wrapper) indents successfully.
So, defying intuition, in order to get an indented output, a libxml2 user must already prepare that before initializing the parser responsible for manipulating the input. I also have a vague memory that there are good "whitespaces are sometimes meaningful in XML" reasons for doing this.
So maybe we can reframe this issue to:
- Allow for passing in options to
Parser
- Document how to indent difficult cases.
Not sure we can do much else with the way libxml2 is... Whether we use the xmlsave
or xmlwriter
serialization API, they both reach the same code path, and both require the blanks to have been discarded as part of parsing the input.
from rust-libxml.
That's fine.
Oddly enough I can confirm that
unsafe {
libxml::bindings::xmlKeepBlanksDefault(0);
}
works with parser.parse_string
and not parser.parse_file
.
Also looking at the kind of files I'm working with I'd like to keep blank lines that were in the original (much like many code formatters do) though I don't currently know how to achieve that.
from rust-libxml.
Right, the parser code is still in a "minimal viable product" shape, you can't customize it or do anything advanced with it. And as you mention, parse_file
even goes out of its way to force the blanks to always be there (code here).
So evolving that to accept some options will allow getting the xmllint behaviour. If you want to do something even better than libxml2's algorithm however, you're probably best off writing your own custom serializer, which may end up less confusing in the long run. The main project I've been porting to Rust which uses libxml2 does exactly that, to ensure it formats the XML exactly per the author's taste (which is what the whitespace considerations largely are).
from rust-libxml.
Yeah that's probably what I'll have to do eventually.
Thanks for the help!
from rust-libxml.
Related Issues (20)
- Implement correct deep copy of nodes/documents
- missing ability to get all attributes including namespace HOT 3
- STATUS_ACCESS_VIOLATION with Evaluate function while parsing xpath [Urgent] HOT 3
- Schema validation errors HOT 1
- Cargo test locking up HOT 3
- Not able to link statically HOT 1
- Encoding issue with libxml 2.11.1, 2.11.2, 2.11.3 (OK with libxml 2.11.0) HOT 1
- All `StructuredError` returned by `SchemaValidationContext::validate_*` are identical HOT 3
- Looking for wellformed check HOT 1
- Error handling HOT 2
- Implement Send for Document HOT 4
- Xpath does not query paths, a little bug in the node.get_contents() HOT 1
- Missing the ability to get an attribute with no namespace HOT 2
- Missing Node::to_string method HOT 3
- FreeBSD instructions HOT 4
- Declare function to xmllib2 using rust-libxml HOT 3
- How to receive errors from xpath queries. HOT 8
- Help Wanted | Vaidate XPath Syntax Independent of Context HOT 6
- Segmentation fault with xml serialisation HOT 4
- creating a text node after add_next_sibling and then also trying to add_next_sibling() it causes mutably shared node Err HOT 1
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 rust-libxml.