Search code examples
validationsitecoreextendrichtext

Extend "Is valid Xhtml" validator for Rich Text Field in Sitecore


The "Is valid Xhtml" validator for Rich Text Field in Sitecore does not accept Html valid attributes like: role, aria, data-... etc.

"Is valid Xhtml" validator issue

Trying to find a solution over the internet led to the majority of answers which were to disable the validator, which makes an area for the content author's error. Also, there were suggestions to make the change directly in the Sitecore file, which also has one drawback - makes upgrades a bit more difficult in the future.

This link has shed some light on the final solution: Override XHTML validation for Rich Text Fields in Sitecore


Solution

  • After some investigation, it was found, that under the hood "Is valid Xhtml" validator is utilizing XSD schema for the validation process. Happily, there is a way to extend it.

    Here are the steps:

    1. Create a customer specifically named XSD Schema file e.g. "[Customer Name] Sitecore xhtml5.xsd", which will contain the next content (e.g. for my case):
        <?xml version="1.0" encoding="UTF-8"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                   xmlns="http://www.w3.org/1999/xhtml"
                   xmlns:html="http://www.w3.org/1999/xhtml"
                   xmlns:svg="http://www.w3.org/2000/svg"
                   xmlns:xlink="http://www.w3.org/1999/xlink"
                   xmlns:mml="http://www.w3.org/1998/Math/MathML"
                   targetNamespace="http://www.w3.org/1999/xhtml"
                   elementFormDefault="qualified">
            <xs:include schemaLocation="Sitecore xhtml5.xsd"/>
    
            <xs:redefine schemaLocation="Sitecore xhtml5.xsd">
                <xs:complexType name="styledFlowContentElement">
                    <xs:complexContent>
                        <xs:extension base="styledFlowContentElement">
                            <xs:attribute name="aria-label" type="xs:string"/>
                            <xs:attribute name="row" type="xs:string"/>
                            <xs:attribute name="data-video-id" type="xs:string"/>
                            <xs:attribute name="aria-describedby" type="xs:string"/>
                            <xs:attribute name="role" type="xs:string"/>
                        </xs:extension>
                    </xs:complexContent>
                </xs:complexType>
            </xs:redefine>
        </xs:schema>
    

    It includes the original xsd file (via "xs:include") and extending necessary element (via "xs:redefine") with additional for my case attributes.

    Place it in MSVS project in the next hierarchy of folders "sitecore\shell\Schemas\[Customer Name] Sitecore xhtml5.xsd". Make sure file's "Build Action" is "Content" (right click -> properties): File Build Action Content

    1. Create patch file:
        <sitecore>
            <settings>
                <setting name="XHtmlSchemaFile" value="/sitecore/shell/schemas/[Customer Name] Sitecore xhtml5.xsd"/>
            </settings>
        </sitecore>
    

    And there are no more errors regarding undefined attributes.