Skip to content
MNB PHPExcelDeveloper Guide
AI

XML library

XML Schema Mapping

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

Level
Advanced
Reading time
9 min
Package
mnb/mnb-phpexcel-xml
Updated
XmlSchemaMapping::from()Xml::read()

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,
];