Magento 2 custom xml schema validation

The question:

This is a follow up of Magento 2 xml validation:
I understood how to map xsi:noNamespaceSchemaLocation in my IDE so I will get real time validation of my xmls.
But what if I have to create my own config file with it’s own validation schema?
What value should I put for xsi:noNamespaceSchemaLocation ?

Right now I have this in class.xml:

<?xml version="1.0"?>
<classes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="class.xsd">
   ....
</classes>

This works nicely, because class.xsd is in the same folder as my class.xml.
But I plan to make my module extensible and actually composed from multiple modules.
Now I use in the additional modules a relative path to the class.xsd file and this seams a bit wrong.

<?xml version="1.0"?>
<classes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Base/etc/umc/class.xsd">
   ....
</classes>

Obviously I cannot use urn:magento:framework... because my module is not in the framework, and I also cannot use urn:magento:module... because mine is not a core module. Or can I use the latter?
Or should I use a custom urn?
And (last one) if I use a custom urn will this be picked up automatically by the command bin/magento dev:urn-catalog:generate or should I dos something special?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It should be urn:magento:module:<Vendor_Module>:<path>
The command will work since it filters out urns that begin with urn:magento
https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Developer/Console/Command/XmlCatalogGenerateCommand.php#L118


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment