how to remove the namespaces from the Element
I am working with org.w3c.xml java library and encountering a few difficulties performing a few tasks:
- I have an Element object; how can I remove namespaces from it an开发者_如何学运维d the predecessors?
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.
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?
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());
精彩评论