MNB PHPExcelDeveloper Guide
XML module

XML Schema Mapping

Map nested elements, attributes, text, and typed fields from external XML schemas into normalized spreadsheet rows.

Updated

LevelAdvancedReading time9 minPackagemnb/mnb-phpexcel
XmlSchemaMapping::from()Xml::read()

What you will learn

  • Define paths from nested source XML to canonical row columns.
  • Read values from attributes, row text, or child elements.
  • Cast strings to integers, decimals, booleans, JSON, dates, or datetimes.

Define a schema mapping#

PHP
$schema = [
    'sheet_tag' => 'catalog',
    'sheet_name_attribute' => 'name',
    'row_tag' => 'product',
    'columns' => [
        'sku' => ['source' => '@sku', 'type' => 'string'],
        'name' => ['source' => 'details/name', 'type' => 'string'],
        'price' => ['source' => 'pricing/amount', 'type' => 'decimal'],
        'active' => ['source' => '@active', 'type' => 'boolean'],
        'released_at' => ['source' => 'released', 'type' => 'datetime'],
    ],
];

$rows = Xml::read('catalog.xml', [
    'xml_schema' => $schema,
])->sheet('Products')->withHeaderRow(1)->rows();

Use explicit mappings for external contracts#

Schema mapping separates the external XML contract from your application’s canonical field names. That makes validation, database insertion, and downstream JSON output stable even when source nesting is awkward.

Keep parser limits in the same configuration#

PHP
$options = [
    'xml_schema' => $schema,
    'max_file_bytes' => 50 * 1024 * 1024,
    'max_source_rows' => 500000,
    'max_depth' => 32,
    'allow_doctype' => false,
];