简体   繁体   English

使用Java删除xml中除replaceall以外的所有命名空间

[英]Remove all the namespaces in xml other than replaceall using java

I want to remove all the namespaces in xml document using java, is there a efficient way to do it other than replace all method? 我想使用Java删除xml文档中的所有名称空间,除了替换所有方法之外,还有一种有效的方法吗?

Thanks in advance. 提前致谢。 Sushma. Sushma。

Removing the namespace declarations using regex processing is probably feasible provided you don't care about the small probability of matching something that isn't actually a namespace declaration (eg inside a comment or CDATA section). 如果您不关心匹配实际上不是名称空间声明的内容(例如,在注释或CDATA部分中)的可能性很小,则使用正则表达式处理删除名称空间声明可能是可行的。 Removng the prefixes from element and attribute names is more tricky. 从元素和属性名称中删除前缀比较棘手。 Why not do it the easy way, using XSLT? 为什么不使用XSLT轻松实现呢? It just needs a simple 2-rule stylesheet: 它只需要一个简单的2规则样式表:

<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="*|@*"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{local-name()}">
    <xsl:value-of select=."/>
  </xsl:attribute>
</xsl:template>

You can add further rules if you find you need to do more than this, eg keeping the namespaces on xml:base or xsi:schemaLocation attributes. 如果发现您还需要做更多的事情,则可以添加其他规则,例如,将名称空间保留在xml:base或xsi:schemaLocation属性上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM