如何从元素中删除命名空间 [英] how to remove the namespaces from the Element

查看:35
本文介绍了如何从元素中删除命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 org.w3c.xml java 库并在执行一些任务时遇到一些困难:

I am working with org.w3c.xml java library and encountering a few difficulties performing a few tasks:

  1. 我有一个 Element 对象;如何从它和前辈中删除命名空间?
  2. 如何在没有命名空间的情况下创建文档?我试过了

  1. I have an Element object; how can I remove namespaces from it and the predecessors?
  2. How can I create a Document without the namespaces? I have tried

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(false);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("C:/Temp/XMLFiles/"+fileName+".xml"));

虽然看起来很有希望,但实际上并不奏效.我仍在获取带有命名空间的文档.

Although it looks promising, it does not really work. I am still getting the doc with namespaces.

如何从元素创建文档?

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc.adoptNode(dataDefinition);

其中 dataDefinition 是一个元素,但它不起作用;我做错了什么?

where dataDefinition is an element, but it didn't work; what am I doing wrong?

推荐答案

尝试使用以下 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

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

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

<小时>

Transformer xformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new FileInputStream("xform.xsl")));
StringWriter writer = new StringWriter();
xformer.transform(new StreamSource(new FileInputStream("input.xml")), new StreamResult(writer));
System.out.println(writer.toString());

这篇关于如何从元素中删除命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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