如何从元素中删除名称空间

发布于 2024-08-18 13:52:50 字数 693 浏览 3 评论 0原文

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

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

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

    虽然看起来很有希望,但实际上并没有什么作用。我仍然收到带有命名空间的文档。

  3. 如何从元素创建文档?

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

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

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

  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.

  3. How do I create a document from an Element?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2024-08-25 13:52:50

尝试使用以下 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());

Try transforming it with the following 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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文