且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 19:03:52

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