XML Schema Mapping
Map nested elements, attributes, text, and typed fields from external XML schemas into normalized spreadsheet rows.
Updated
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#
$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#
$options = [
'xml_schema' => $schema,
'max_file_bytes' => 50 * 1024 * 1024,
'max_source_rows' => 500000,
'max_depth' => 32,
'allow_doctype' => false,
];Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue