Namespaces in XML based payment files

XML documents  use namespaces to define and distinguish different elements and attributes within the document. They help avoid naming conflicts when multiple XML vocabularies or schemas are used within the same document or when different organizations or standards contribute to the structure of the XML file. Namespaces are typically defined using a Uniform Resource Identifier (URI), which is a globally unique identifier. This URI serves as the namespace's name and is associated with a prefix that is used throughout the XML document. You can also define a default namespace without a prefix, which applies to all elements and attributes within a specific scope unless overridden by a prefix.

Here's a simplified sample of a pain.001 message to demonstrate the typical use of namespaces: 

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.09" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CstmrCdtTrfInitn>
    <GrpHdr>
      <MsgId>SEPA123456789</MsgId>
      <CreDtTm>2023-09-25T15:30:00</CreDtTm>
      <NbOfTxs>2</NbOfTxs>
      <CtrlSum>250.00</CtrlSum>
      <InitgPty>
        <Nm>Company ABC</Nm>
      </InitgPty>
    </GrpHdr>
    <PmtInf>
    ... 
    </PmtInf>
  </CstmrCdtTrfInitn>
</Document>

The namespaces are defined in the "Document" element attributes. The main payment message schema is defined in the default namespace, as shown in the following snippet, thus there's no need to use prefixes in the rest of the file as all used elements are defined in the payment message schema. 

xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.09"

Additionally, a named namespace is declared in the sample above, as highlighted in the next snippet. It refers to a generic XML declaration, of which you can learn more about here. None of the following elements in the sample message are directly from the namespace, so there is no need to use the prefix. 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

In case an element is utilized from a named namespace, it is necessary to provide a prefix before the element name. For instance, if the payment message schema is declared under a named namespace "ns1", the structure of the file would resemble the following. 

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Document xmlns:ns1="urn:iso:std:iso:20022:tech:xsd:pain.001.001.09">
  <ns1:CstmrCdtTrfInitn>
    <ns1:GrpHdr>
      <ns1:MsgId>SEPA123456789</ns1:MsgId>
  </ns1:GrpHdr>
 </ns1:CstmrCdtTrfInitn>
</ns1:Document>

In order to correctly reference elements declared in the payment message schema, it is necessary to include the appropriate prefix before the element name. This ensures that the elements are properly identified and associated with the relevant schema.