I want to remove all the namespace attributes from the following XML.

<md:EntityDescriptor xmlns="urn:_" xmlns:md="_"></md:EntityDescriptor>

I have tried to remove xmlns using following command which does not change the input in anyway.

xmlstarlet ed \
-N n="urn:oasis:names:tc:SAML:2.0:metadata" \
-d '/md:EntityDescriptor/@*[n:name()="xmlns"]' \
<<< '<md:EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:md="_"></md:EntityDescriptor>' \
> ttt.xml

Expected output:


2 Answers 2


I have found a couple of XSLT solutions to do this, both of which can conveniently be processed with xmlstarlet:

  1. How to remove the namespace and its prefixes in an XML file using XSLT? - IBM
  2. XSLT: Remove namespace prefix from elements - StackOverflow

In my worked example below, I've used the IBM code and saved it into the file xslt_ibm:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- https://www.ibm.com/support/pages/how-remove-namespace-and-its-prefixes-xml-file-using-xslt -->
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/|comment()|processing-instruction()">
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>

If you consider your XML file to be saved in the file file.xml then this command will rename each element and attribute out of its namespace:

xmlstarlet transform xslt_ibm file.xml

Input (file.xml)

<md:EntityDescriptor xmlns="urn:_" xmlns:md="_"></md:EntityDescriptor>


<?xml version="1.0"?>

Using Andrey Kislyuk's xq, an XML-parsing wrapper around jq (installed together with yq):

xq -x 'walk(del( .["@xmlns"]?, .["@xmlns:md"]? ))' file

This walks all nodes in the XML document structure and deletes all xmlns and xmlns:md attributes wherever these are found.

Given some input document,

<?xml version="1.0"?>
<root test="val">
  <md:EntityDescriptor xmlns="urn:_" xmlns:md="_"/>

... this would output

<root test="val">

You get in-place editing with the --in-place or -i option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .