views:

106

answers:

1

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?

A: 

Try transforming it with the following XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<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());
Kevin