处理XML元素属性的顺序 [英] Manipulate order of XML element attributes

查看:63
本文介绍了处理XML元素属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过很多帖子,人们问到有关对XML元素强制执行某些顺序的命令的情况,并且普遍的回答是它不合法/必需/允许/相关/其他.

I have read many post where people have asked about enforcing some order of attributes to an XML element and the general response is that it's not legal/required/allowed/relevant/other.

我不需要任何回应,说我不在乎属性顺序,因此,如果您是我的观点,请不要回复.

I am not looking for any response saying I shouldn't care about attribute order, so please don't reply if that's your view.

我有一个真正的问题需要解决.大型公司产品在其产品的最新版本中将以下两个元素视为不同

I have a real problem which needs a solution. A large corporate product treats the following two elements as different in the latest version of their product

<objquestion allowmultiple="true" id="7432" idtext="7433" idvar="7429" parent="7430" questiontype="multchoice">

<objquestion id="7432" idtext="7433" idvar="7429" parent="7430" questiontype="multchoice" allowmultiple="true">

尤其是,如果"allowmultiple"属性在"questiontype"之后,则它将用作问题类型的修饰符.如果在此之前,则将其忽略-不应.

Particularly, if the "allowmultiple" attribute is after the "questiontype" it acts as a modifier to the question type. If it's before, it's ignored - it shouldn't be.

因此,他们不太可能在短期内修复其产品.

So, they are unlikely to fix their product in the short term.

我正在使用

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
doc = dbf.newDocumentBuilder().parse(new InputSource(path));

和内部实现将对DOM节点映射中的属性进行排序.当将其写回到文件时,它将按照它们现在排序的顺序写入属性.我有很多使用XPath处理Document对象的代码.

and the internal implementation will sort the attributes in the DOM node map. When it's written back to the file it writes the attributes in their now sorted order. I have a lot of code that is playing around with the Document object using XPath.

处理完文档后,我现在用

When I have finished manipulating the Document I currently write it back with

Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(lc.getDocument()), new StreamResult(new File(paths[1])));

我需要做的是确保在问题类型之后写入allowmultiple属性.

What I need to be able to do is to ensure the allowmultiple attribute is written AFTER the questiontype.

我试图了解我是否可以影响用于写回DOM树的序列化,或者我是否可以简单地替换一个最初不将属性解析为排序映射的实现.我想两者都行得通,但是我还无法找出如何做到这一点.

I have tried to understand if I can either affect the serialisation used to write the DOM tree back or if I can simply substitute a different implementation that does not parse the attributes into a sorted map initially. I guess both would work, but I've not been able to find out how to do this.

我看过LSSerializer,但不确定如何截获该特定元素.我是否需要扩展FileOutputStream并寻找一些东西?

I looked at LSSerializer, but I am not sure how I can intercept that particular element. Would I have to extend a FileOutputStream and look for something?

我已经读过SAX可能不会进行初始排序,但是我需要能够在没有太多新代码的情况下加入解析器,并且对于整个XML世界而言,它并不是那么强大.

I have read that SAX might not do the initial sorting, but I need to be able to drop in the parser without much new code and am not so strong with the whole XML world.

有人可以建议一种方法吗?

Can anyone suggest a way to do this?

推荐答案

下一个Saxon版本(即将发布9.5)具有序列化属性,可用于控制属性顺序.它是为合法用例添加的(例如,它可以提高人们的可读性,使id属性始终排在第一位),我有点遗憾,由于它的不称职和不负责任,最终将被用于像您这样的用例中是大公司雇用的程序员,但事实是:如果它解决了一个问题,我不会哭.

The next Saxon release (9.5, due imminently) has a serialization attribute that allows you to control attribute order. It was added for legitimate use cases (it can improve human readability to have id attributes always come first, for example), and I slightly regret that it's going to end up being used for use cases like yours that result from the incompetence and irresponsibility of the programmers employed by large corporations, but so be it: if it solves a problem, I won't cry.

这篇关于处理XML元素属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆