Stephen Gilmore

Exclude XML namespace prefixes in XSLT

Workday October 29th, 2023 1 minute read.

I've been working on a PECI integration that sends an XML vendor to the file. Today I received the requirement: "could you remove the namespace?"

Answer: "Of course, it's easy!"

The problem

Let's take a PECI XML -> XML file transformation as an example. The resulting XML file will have the following at the top:

<?xml version='1.0' encoding='UTF-8'?>
<Person xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                xmlns:peci="urn:com.workday/peci">

The xmlns:xs and xmlns:peci namespaces are causing downstream issues and need to be removed.

The solution

To remove these on the output, add exclude-result-prefixes="ns1 ns2" to the xsl:stylesheet tag attributes.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:peci="urn:com.workday/peci"
    xmlns:etv="urn:com.workday/etv" version="2.0" exclude-result-prefixes="peci xs">
...