30

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?

46

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

  • If Phpstorm isn't in the same machine as the code, I think it's still safe to copy that file somewhere near magento code, execute the command provided, and then copy back to dev environnement. – medmek Jun 21 '18 at 16:48
10

So magento changed the XSD URI to use Urns - These need to be configured in PHP Storm in order for schema validation to work again.

Source : http://alankent.me/2015/10/07/xml-schema-resolution-in-php-storm-with-urns-quick-note/

The URN resolution code is built into Magento directly. Luckily, it’s pretty easy to associate URNs with real files in your local site in PHP Strom.

This is what you will see after loading a newever magento2 version with urns. Notice the URN is in red because PHP Storm does not know how to resolve it.

enter image description here

Put the cursor in the URN then click on the red light bulb to the left that will appear and select “Manually Setup External Resource” from the list.

enter image description here

Scroll down the list of XSD files that PHP Storm has automatically located, and select the appropriate file. The red URN will now be shown in green, and XML validation is back in effect.

5

URN generation is supported by Magento 2 plugin for PHP Storm, and is only one of many useful features it provides to Magento 2 developers.

When plugin is installed in the PHP Storm, go to Preferences / Other Settings / Magento2 plugin and click Regenerate URN map.

3

Eclipse IDE Users:

Pros:

  • Eclipse will recognize the path to the XSDs, and all the benefits which that brings.

Cons:

  • Configuration is at a workspace scope, not project scope.
  • Manually have to set it up.
  • If Magento adds new XSDs in future versions, or changes the path, you'll have to manually manage these changes.

For the efficiently lazy: I've provided a source block at the end of this answer if you prefer to jump to that and just copy/paste it all.

Environment these instructions were written from:

  • Magento EE 2.1.3
  • Fedora 25 Workstation
  • Eclipse Neon.2 (4.6.2)
  • Eclipse Installed Software: Eclipse Web Developer Tools
    • Version: 3.8.1.v201609072018
    • ID: org.eclipse.wst.web_ui.feature.feature.group
    • Provider: Eclipse Web Tools Platform

Instructions:

Using the Magento CLI generate your mappings using the same command intended for PHPStorm. The exact name/location of the output file is not important as you'll just be using this as a reference when setting up your Eclipse Workspace configuration:

php -f bin/magento dev:urn-catalog:generate urn_mappings.xml

Once the file is generated, open it up with any program other than Eclipse, as you'll need to reference this file while setting up your Workspace settings. Since having Window -> Preferences open blocks access to Eclipse's editor, this is why I say use a different editor to view it.

The file will look similar to the following (example below has been truncated after first <resource/> node). "PATH_TO_PROJECT_ROOT" of course, will differ for you and actually be your path to your project root.

<?xml version="1.0"?>
<project version="4">
  <component version="2" name="ProjectRootManager"/>
  <component name="ProjectResources">
    <resource url="urn:magento:module:Magento_Store:etc/config.xsd" location="/PATH_TO_PROJECT_ROOT/vendor/magento/module-store/etc/config.xsd"/>
...

Next you'll want to setup each of these mappings up in Eclipse. Open up Eclipse and navigate to Window -> Preferences -> XML -> XML Catalog

  • Click "Add" to open the "Add XML Catalog Element" window.
  • Add a "Catalog Entry" (Default entry type in mentioned version of eclipse)
  • Referencing your the <resource/> node in your URN mappings file:
    • Copy "location" from the <resource/> node into the "Location" field of the form.
    • Copy "url" from the <resource/> node into the "Key" field of the form.
    • (Just some additional info) "Key type" on the form most likely has now automatically changed to "Namespace name"

Repeat the above steps for each and every <resource/> node of your URN mappings file.

¿¿¿Magento EE 2.1.3 Bug???

My copy of Magento seems to generate the XSD path for the vendor/magento/module-persistent/etc/persistent.xsd file twice. Once with a /./ in the paths, and once without. Eclipse will complain about having periods in your path, so just ignore this one Magento has generated... and use the one without.

Ignore:

<resource url="urn:magento:module:Magento_Persistent:etc/./persistent.xsd" location="/PATH_TO_PROJECT_ROOT/vendor/magento/module-persistent/etc/./persistent.xsd"/>

Use Instead

<resource url="urn:magento:module:Magento_Persistent:etc/persistent.xsd" location="/PATH_TO_PROJECT_ROOT/vendor/magento/module-persistent/etc/persistent.xsd"/>

That's all. After this very tedious setup process Eclipse will now recognize the paths to your XSD files. All those little warnings will go away from your Project Explorer tree, and type hinting will also now work! w00t w00t!

Too much work bro?? Here let me do it for you...

As promised, for the "efficiently lazy", here is instructions for simply copy pasting the end result to your Eclipse's Workspace configuration.

  • Using your preferred file manager, navigate to the root of your Eclipse's workspace directory. If on linux, and you're just using the default workspace location, it is most likely ~/workspace.
  • From this directory navigate to .metadata/.plugins/org.eclipse.wst.xml.core/
  • In this directory you'll find a file user_catalog.xml which holds all your User Specified Entries I've explained how to setup manually above.
    • I never actually confirmed beforehand, if this file exists already when there are no user specified entries. So if it is not there, create it.
    • If you had no prior User Specified Entries, you can copy/paste the entire contents of the XML block below. Else copy just the <uri/> nodes to within the existing <catalog/> node, so as to not overwrite your existing entries.
    • After Copy/Pasting, replace "PATH_TO_PROJECT_ROOT" with your actual path to your project root.

Source:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
  <uri name="urn:magento:module:Magento_Store:etc/config.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-store/etc/config.xsd"/>
  <uri name="urn:magento:framework:ObjectManager/etc/config.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/ObjectManager/etc/config.xsd"/>
  <uri name="urn:magento:module:Magento_Config:etc/system_file.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-config/etc/system_file.xsd"/>
  <uri name="urn:magento:framework:App/etc/routes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/App/etc/routes.xsd"/>
  <uri name="urn:magento:framework:Acl/etc/acl.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Acl/etc/acl.xsd"/>
  <uri name="urn:magento:module:Magento_Backend:etc/menu.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-backend/etc/menu.xsd"/>
  <uri name="urn:magento:framework:Event/etc/events.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Event/etc/events.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/page_configuration.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/page_configuration.xsd"/>
  <uri name="urn:magento:module:Magento_Webapi:etc/webapi.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-webapi/etc/webapi.xsd"/>
  <uri name="urn:magento:module:Magento_Cron:etc/crontab.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-cron/etc/crontab.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/layout_generic.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/layout_generic.xsd"/>
  <uri name="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-ui/etc/ui_configuration.xsd"/>
  <uri name="urn:magento:framework:View/PageLayout/etc/layouts.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/PageLayout/etc/layouts.xsd"/>
  <uri name="urn:magento:framework:Config/etc/theme.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Config/etc/theme.xsd"/>
  <uri name="urn:magento:framework:Mview/etc/mview.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Mview/etc/mview.xsd"/>
  <uri name="urn:magento:framework:Api/etc/extension_attributes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Api/etc/extension_attributes.xsd"/>
  <uri name="urn:magento:framework:Indexer/etc/indexer.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Indexer/etc/indexer.xsd"/>
  <uri name="urn:magento:module:Magento_Customer:etc/sections.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-customer/etc/sections.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/page_layout.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/page_layout.xsd"/>
  <uri name="urn:magento:module:Magento_Cron:etc/cron_groups.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-cron/etc/cron_groups.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/catalog_attributes.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/product_types.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/product_types.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/product_types_merged.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/product_types_merged.xsd"/>
  <uri name="urn:magento:framework:Config/etc/view.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Config/etc/view.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/product_options.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/product_options.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/product_options_merged.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/product_options_merged.xsd"/>
  <uri name="urn:magento:module:Magento_Widget:etc/widget.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-widget/etc/widget.xsd"/>
  <uri name="urn:magento:module:Magento_Eav:etc/eav_attributes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-eav/etc/eav_attributes.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/page_types.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/page_types.xsd"/>
  <uri name="urn:magento:framework:Cache/etc/cache.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Cache/etc/cache.xsd"/>
  <uri name="urn:magento:module:Magento_Ui:etc/data_source.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-ui/etc/data_source.xsd"/>
  <uri name="urn:magento:framework:Validator/etc/validation.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Validator/etc/validation.xsd"/>
  <uri name="urn:magento:module:Magento_Directory:etc/zip_codes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-directory/etc/zip_codes.xsd"/>
  <uri name="urn:magento:module:Magento_Email:etc/email_templates.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-email/etc/email_templates.xsd"/>
  <uri name="urn:magento:module:Magento_Sales:etc/sales.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-sales/etc/sales.xsd"/>
  <uri name="urn:magento:framework:DataObject/etc/fieldset.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/DataObject/etc/fieldset.xsd"/>
  <uri name="urn:magento:module:Magento_Sales:etc/pdf_file.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-sales/etc/pdf_file.xsd"/>
  <uri name="urn:magento:framework:App/etc/resources.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/App/etc/resources.xsd"/>
  <uri name="urn:magento:module:Magento_Customer:etc/address_formats.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-customer/etc/address_formats.xsd"/>
  <uri name="urn:magento:module:Magento_Integration:etc/integration/integration.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-integration/etc/integration/integration.xsd"/>
  <uri name="urn:magento:module:Magento_Ui:etc/ui_definition.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-ui/etc/ui_definition.xsd"/>
  <uri name="urn:magento:module:Magento_ImportExport:etc/import_merged.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-import-export/etc/import_merged.xsd"/>
  <uri name="urn:magento:module:Magento_ImportExport:etc/import.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-import-export/etc/import.xsd"/>
  <uri name="urn:magento:module:Magento_ImportExport:etc/export.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-import-export/etc/export.xsd"/>
  <uri name="urn:magento:module:Magento_ImportExport:etc/export_merged.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-import-export/etc/export_merged.xsd"/>
  <uri name="urn:magento:module:Magento_Payment:etc/payment.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-payment/etc/payment.xsd"/>
  <uri name="urn:magento:framework:Search/etc/search_engine.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Search/etc/search_engine.xsd"/>
  <uri name="urn:magento:framework:Search/etc/search_request.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Search/etc/search_request.xsd"/>
  <uri name="urn:magento:module:Magento_Logging:etc/logging.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-logging/etc/logging.xsd"/>
  <uri name="urn:magento:framework:/App/etc/routes.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework//App/etc/routes.xsd"/>
  <uri name="urn:magento:module:Magento_WebsiteRestriction:etc/webrestrictions.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-website-restriction/etc/webrestrictions.xsd"/>
  <uri name="urn:magento:module:Magento_Logging:etc/logging_file.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-logging/etc/logging_file.xsd"/>
  <uri name="urn:magento:module:Magento_GiftRegistry:etc/giftregistry.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-gift-registry/etc/giftregistry.xsd"/>
  <uri name="urn:magento:module:Magento_Config:etc/system_include.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-config/etc/system_include.xsd"/>
  <uri name="urn:magento:module:Magento_Paypal:etc/rules.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-paypal/etc/rules.xsd"/>
  <uri name="urn:magento:module:Magento_Persistent:etc/persistent.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-persistent/etc/persistent.xsd"/>
  <uri name="urn:magento:module:Magento_VersionsCms:etc/menu_hierarchy.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-versions-cms/etc/menu_hierarchy.xsd"/>
  <uri name="urn:magento:module:Magento_Elasticsearch:etc/esconfig.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-elasticsearch/etc/esconfig.xsd"/>
  <uri name="urn:magento:framework-foreign-key:etc/constraints.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework-foreign-key/etc/constraints.xsd"/>
  <uri name="urn:magento:framework-message-queue:etc/queue.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework-message-queue/etc/queue.xsd"/>
  <uri name="urn:magento:framework:Communication/etc/communication.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Communication/etc/communication.xsd"/>
  <uri name="urn:magento:module:Magento_AdminGws:etc/admingws.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-admin-gws/etc/admingws.xsd"/>
  <uri name="urn:magento:module:Magento_Support:etc/report.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-support/etc/report.xsd"/>
  <uri name="urn:magento:module:Magento_Catalog:etc/product_types_base.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-catalog/etc/product_types_base.xsd"/>
  <uri name="urn:magento:module:Magento_Sales:etc/pdf.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-sales/etc/pdf.xsd"/>
  <uri name="urn:magento:module:Magento_Integration:etc/integration/integration_base.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-integration/etc/integration/integration_base.xsd"/>
  <uri name="urn:magento:framework:Data/etc/argument/types.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Data/etc/argument/types.xsd"/>
  <uri name="urn:magento:module:Magento_Ui:etc/ui_components.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-ui/etc/ui_components.xsd"/>
  <uri name="urn:magento:module:Magento_Widget:etc/types.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/module-widget/etc/types.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/elements.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/elements.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/head.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/head.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/body.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/body.xsd"/>
  <uri name="urn:magento:framework:View/Layout/etc/html.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/View/Layout/etc/html.xsd"/>
  <uri name="urn:magento:framework:Search/etc/requests.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Search/etc/requests.xsd"/>
  <uri name="urn:magento:framework-message-queue:etc/queue_base.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework-message-queue/etc/queue_base.xsd"/>
  <uri name="urn:magento:framework-message-queue:etc/queue_base_deprecated.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework-message-queue/etc/queue_base_deprecated.xsd"/>
  <uri name="urn:magento:framework:Module/etc/module.xsd" uri="file:///PATH_TO_PROJECT_ROOT/vendor/magento/framework/Module/etc/module.xsd"/>
</catalog>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.