C# 合并 xml 中的命名空间引用
我有由atom 格式化程序格式化的xml。 原子格式化程序似乎多次指定内联命名空间。
有什么办法可以轻松整合这些。 下面的示例显示了为每个属性指定了三次的命名空间。 这太可怕了。
我希望文档顶部有前缀,并且文档中没有命名空间(只是前缀)。是否有编写器或格式化程序选项可以实现此目的?
<property p3:name="firstname" xmlns:p3="http://a9.com/-/opensearch/extensions/property/1.0/" xmlns="http://a9.com/-/opensearch/extensions/property/1.0/">Drikie</property>
谢谢克雷格
。
I have xml formatted by the atom formatter.
The atom formatter seems to specify namespaces inline multiple times.
Is there any way to easily consolidate these.
The example below shows namespaces specified three times for each property.
This is horrible.
I would like prefixes at the top of the document and no namespaces in the document (just prefixes). Is there a writer or formatter option to achieve this?
<property p3:name="firstname" xmlns:p3="http://a9.com/-/opensearch/extensions/property/1.0/" xmlns="http://a9.com/-/opensearch/extensions/property/1.0/">Drikie</property>
Thanks
Craig.
生成这种更紧凑格式的最简单方法是在 XML 文档上应用以下 XSLT 转换:
例如,当应用于以下 XML 文档时(根据您的问题):
产生了想要的结果:
请注意:
命名空间声明不能进一步提升到具有绑定相同前缀的声明的元素之上到另一个命名空间。
将命名空间声明提升到祖先元素可能会增加解析的 XML 文档的大小,因为所有命名空间节点都会向下传播到所有后代节点,其中一些节点可能根本不需要该命名空间.
The easiest way to produce this more compact format is to apply the following XSLT transformation on your XML document:
For example, when applied on the following XML document (based on your question):
the wanted result is produced:
Do note:
A namespace declaration cannot be promoted further above an element that has a declaration that binds the same prefix to another namespace.
Promoting a namespace declaration to an ancestor element may increase the size of the parsed XML document, because all namespace nodes are propagated down to all descendent nodes, some of which may not need at all that namespace.