4 ms·
Okay, so what am I supposed to use if I want to transform a XML file from one format to another because two different systems needs XML for input /output and th
by ohyesyodo 12y ago
Okay, so what am I supposed to use if I want to transform a XML file from one format to another because two different systems needs XML for input /output and they have different fixed formats?
- crdoconnor 12y agoI would use a language that has a decent XML parser (e.g. python + lxml) for input and a decent templating language (e.g. jinja2) for output. Assuming it was a simple transformation, the python parsing code could be under 10 lines. Most of what you wrote would be templating. It would be 98% declarative. If it got complicated though (e.g. you're doing some aggregation or something), the python bit would grow but it probably never end up looking that horrendous, unlike XSLT. The same pattern could be applied to many other language ecosystems. You just need to make sure you get the best XML parsing library and the best templating language.
- ilitirit 12y agoYou could do the exact same thing using XSLT. eg. var result = new XSLTProcessor().importStylesheet(xsl).transform(....); It's a bit of a pointless example because it really depends on the transformations you need. I'm sure in some cases XSLT would be better for the job, and in other cases another language. Most of the time it would generally just depend on your environment, available tools and skillset.
- crdoconnor 12y ago>I'm sure in some cases XSLT would be better for the job In some (simple transformation) cases XSLT would be no worse, but mostly it would be worse. I can't see it being clearer or easier to maintain under any circumstances. Once your code evolves toward doing anything mildly complicated you'll wish you never made your transformation in xslt.
- jgalt212 12y agoI don't know if you're trolling, but here's how I'd do it in Python import xml.etree.ElementTree as ET tree = ET.parse('data.xml') new_tree = my_transform(tree) new_tree.write("output.xml")
- ohyesyodo 12y agoI'm not sure if you are trolling, but you left out the actual transform. In my experience, XSLT feels optimized towards transforms, which most other languages aren't. I also dislike XSLT, but whenever I do things like this in Python, C# or C++ it tends to get more messy then my XLTS when the transforms are nontrivial.
- jgalt212 12y agoI'm getting downvotes here? Obviously, not from ohyesyodo b/c he does not have enough karma to allocate downvotes.