Skip to content

Instantly share code, notes, and snippets.

Created June 22, 2020 13:19
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
XSLT for removing unused namespaces from an XML file.
Author: Dimitre Novatchev
License: CC BY-SA,
xmlstarlet tr remove-unused-namespaces.xslt -
Processes stdin and writes the result to stdout.
<xsl:stylesheet version="1.0"
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" priority="-2">
<xsl:apply-templates select="node()|@*"/>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:variable name="vtheElem" select="."/>
<xsl:for-each select="namespace::*">
<xsl:variable name="vPrefix" select="name()"/>
<xsl:if test=
substring-before(name(),':') = $vPrefix)
@*[substring-before(name(),':') = $vPrefix]
<xsl:copy-of select="."/>
<xsl:apply-templates select="node()|@*"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment