我有由原子格式化程序格式化的 xml。
原子格式化程序似乎多次内联指定命名空间。
有什么方法可以轻松地整合这些。
下面的示例显示了为每个属性指定了 3 次的命名空间。
这太可怕了。
我想要文档顶部的前缀,并且文档中没有命名空间(只是前缀)。是否有编写器或格式化程序选项来实现这一目标?
<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>
谢谢
克雷格。
最佳答案
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.
生成这种更紧凑格式的最简单方法是对 XML 文档应用以下 XSLT 转换 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()[not(self::*)]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="descendant::*/namespace::*"/>
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
例如,当应用于以下 XML 文档 时(基于你的问题):
<t xmlns="http://a9.com/-/opensearch/extensions/property/1.0/">
<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>
</t>
产生了想要的结果 :
<t
xmlns="http://a9.com/-/opensearch/extensions/property/1.0/"
xmlns:p3="http://a9.com/-/opensearch/extensions/property/1.0/">
<property p3:name="firstname">Drikie</property>
</t>
请注意 :
关于C# 在 xml 中合并命名空间引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3385069/