Magento 2 possède des XSD qui permettent de définir les structures que devront respecter vos fichiers XML.
Il existe un outil simple mais efficace qui, en fournissant le XSD, vous donnera un XML d’exemple.
Pratique pour avoir la visualisation d’un fichier de config par exemple.
Rendez-vous sur : http://xsd2xml.com/
Prenons pas exemple le XSD suivant :
/lib/internal/Magento/Framework/App/etc/routes.xsd
Voici son contenu :
<?xml version="1.0" encoding="UTF-8"?> <!-- /** * This schema should be applied for validation of separate router.xml files * * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="config"> <xs:complexType> <xs:sequence> <xs:element name="router" minOccurs="1" maxOccurs="unbounded"> <xs:annotation> <xs:documentation> Router collects routes, parses request and performs matching of request to route. </xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:choice> <xs:element name="route" minOccurs="1" maxOccurs="unbounded" type="routeType"> <xs:unique name="uniqueModuleName"> <xs:annotation> <xs:documentation>Module name should be unique for route</xs:documentation> </xs:annotation> <xs:selector xpath="module" /> <xs:field xpath="@name" /> </xs:unique> </xs:element> </xs:choice> </xs:sequence> <xs:attribute name="id" use="required" type="routerIdType" /> </xs:complexType> <xs:unique name="uniqueRouteId"> <xs:annotation> <xs:documentation>Route id should be unique</xs:documentation> </xs:annotation> <xs:selector xpath="route" /> <xs:field xpath="@id" /> </xs:unique> <xs:unique name="uniqueRouteFrontName"> <xs:annotation> <xs:documentation>Route front name should be unique</xs:documentation> </xs:annotation> <xs:selector xpath="route" /> <xs:field xpath="@frontName" /> </xs:unique> </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="uniqueRouterId"> <xs:annotation> <xs:documentation>Router id should be unique in file</xs:documentation> </xs:annotation> <xs:selector xpath="router" /> <xs:field xpath="@id" /> </xs:unique> </xs:element> <xs:simpleType name="routerIdType"> <xs:annotation> <xs:documentation> Unique router identifier. </xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z0-9\-_]{3,}" /> </xs:restriction> </xs:simpleType> <xs:complexType name="routeType"> <xs:annotation> <xs:documentation> Route declares requests to process with desired priority and points a module to perform it. </xs:documentation> </xs:annotation> <xs:sequence> <xs:choice> <xs:element name="module" minOccurs="1" maxOccurs="unbounded" type="moduleType" /> </xs:choice> </xs:sequence> <xs:attribute name="id" use="required" type="routeIdType" /> <xs:attribute name="frontName" use="optional" type="routeFrontNameType" /> </xs:complexType> <xs:simpleType name="routeIdType"> <xs:annotation> <xs:documentation> Unique route identifier. </xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z0-9_]{3,}" /> </xs:restriction> </xs:simpleType> <xs:complexType name="moduleType"> <xs:annotation> <xs:documentation> Module is subscribed to process request with corresponding route front name. </xs:documentation> </xs:annotation> <xs:attribute name="name" use="required" type="moduleNameType" /> <xs:attribute name="before" use="optional" type="beforeAfterType" /> <xs:attribute name="after" use="optional" type="beforeAfterType" /> </xs:complexType> <xs:simpleType name="moduleNameType"> <xs:annotation> <xs:documentation> Module name. For example, Magento_User. </xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z0-9_]{3,}" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="beforeAfterType"> <xs:annotation> <xs:documentation> Attributes "before" and "after" help you to specify a priority of routes matching. </xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z0-9_]{3,}" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="routeFrontNameType"> <xs:annotation> <xs:documentation> URL part associated with route. </xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:pattern value="[A-Za-z0-9_\-]{3,}" /> </xs:restriction> </xs:simpleType> </xs:schema>
Et après l’avoir inséré dans l’outil de conversion, voici le XML généré :
<?xml version="1.0" encoding="utf-8"?> <config> <router id="str1234"> <route id="str1234" frontName="str1234"> <module name="str1234" before="str1234" after="str1234" /> </route> </router> </config>
Générer un XML d’exemple à partir du XSD