Magento 2 XML Validation – Step-by-Step Guide

magento2validationxml

For a while Magento 2 had a way to validate (and autocomplete) any xml by using an XSD file.
For examplemodule.xml looked like this:

<?xml version="1.0">
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    ....
</config>

but now module.xml looks like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    ....
</config>

so the xsi:noNamespaceSchemaLocation attribute contains urn:magento:framework:.

I know that this has nothing to do with the server side validation of the xmls. This one still works.
But what can I do to get back my validation in my IDE (PHPStorm in my case, but that's not important, it can be anything else) in order to recognize the path to the xsd?
Should I still use the old relative path while building my modules? Or is there a better way of doing it?

Best Answer

Check updates to the GitHub today, Magento 2 introduced new command to automatically generate all the URN resolutions for the PhpStorm.

To use it:

  • have Magento installed on the same machine as PhpStorm
  • go to the root directory
  • execute command bin/magento dev:urn-catalog:generate .idea/misc.xml where .idea/misc.xml is path to your PhpStorm misc.xml file

All of the URNs should be magically resolved now!

Check them under PhpStorm->Preferences -> Languages&Frameworks -> Schemas and DTDs

PhpStorm 9.0 (windows) : File -> Settings -> Languages&Frameworks -> Schemas and DTDs

Note: Make sure your path to the magento2 installation in phpstorm is the same, or update the paths in the misc.xml file.

  • restart phpstorm