SEO Title: Day 2: Generate UBL XML Structure for IRB e-Invoice #ubl #xmlbuilder
Focus Keyphrase: IRB e-invoice integration
Meta Description: Generate UBL 2.1-compliant XML invoices for IRB e-invoice integration in Laravel. Learn to construct a proper XML structure with required namespaces and tags.
Creating a UBL 2.1-Compliant XML Invoice
IRB requires all submitted e-Invoices to follow the UBL 2.1 standard. That means your XML must follow strict schema rules, use the correct namespaces, and follow element ordering. Let’s generate a basic but valid <Invoice>
XML using Laravel and DOMDocument
.
Step 1: Create the XML Generator Class
Create app/Services/IRB/IRBInvoiceXmlGenerator.php
:
namespace App\Services\IRB;
use DOMDocument;
class IRBInvoiceXmlGenerator
{
public function generate(array $invoiceData): string
{
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
// Create root element
$invoice = $doc->createElementNS(
'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
'Invoice'
);
// Add namespaces
$invoice->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cac',
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
$invoice->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cbc',
'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
$doc->appendChild($invoice);
// Add basic elements
$cbc = 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2';
$invoice->appendChild($doc->createElementNS($cbc, 'cbc:ID', 'INV-1001'));
$invoice->appendChild($doc->createElementNS($cbc, 'cbc:IssueDate', date('Y-m-d')));
$invoice->appendChild($doc->createElementNS($cbc, 'cbc:InvoiceTypeCode', '01')); // 01 = Invoice
return $doc->saveXML();
}
}
Step 2: Create a Test Route in Laravel
In routes/web.php
:
use App\Services\IRB\IRBInvoiceXmlGenerator;
Route::get('/irb/xml-preview', function () {
$xml = app(IRBInvoiceXmlGenerator::class)->generate([]);
return response($xml, 200)->header('Content-Type', 'application/xml');
});
Now visit http://localhost:8000/irb/xml-preview and you’ll see your first valid UBL 2.1 skeleton invoice.
Step 3: Add Buyer & Supplier Details
Tomorrow we’ll expand this by adding full company data, tax category codes, address info, and invoice lines using cac:AccountingSupplierParty
, cac:AccountingCustomerParty
, and cac:InvoiceLine
.
UBL Resources for Reference
Coming Up in Day 3
We’ll continue the UBL generation by adding supplier and buyer identities, tax categories, and address components — exactly as IRBM requires for MyInvois compliance.
Tags: #IRBIntegration #UBL21 #Laravel #EInvoiceXML #MyInvois