Search code examples
xmlxsdrestriction

Defining and reusing restriction in XSD


In my XSD, I have a restriction that I use frequently. The same restriction is used in several places and it could be missed when updating it. I understand that there are tools (find/replace) but I think it is nicer to have these restrictions defined globally. This way, we only have to change it in one place in stead of x-times.

An extra problem I have, is that the name of the element is always different and there is no way to change it (e.g. long_summary, short_summary, ...)

Setup of the XSD

<xs:schema>
    <xs:complexType name="eventType">
        <xs:sequence>
            <xs:element name="short_summary">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value="([\p{L}\p{M}\p{N}\p{P}\p{Z}\p{S}\p{C}]+)?"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="long_summary">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value="([\p{L}\p{M}\p{N}\p{P}\p{Z}\p{S}\p{C}]+)?"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
         </xs:sequence> 
    </xs:complexType>
</xs:schema>

Basically I want to define the restriction ([\p{L}\p{M}\p{N}\p{P}\p{Z}\p{S}\p{C}]+)? only once and reuse it for short_summary and long_summary.

Any advice or directions are appreciated. In the meantime, I will look further and if I find the answer, I will put it here.


Solution

  • The answer to my question:

    Global restriction

    <xs:simpleType name="Text">
            <xs:restriction base="xs:string">
                <xs:pattern value="([\p{L}\p{M}\p{N}\p{P}\p{Z}\p{S}\p{C}]+)?"/>
            </xs:restriction>
    </xs:simpleType>
    

    Used in XSD

    ....
    <xs:element name="short_summary" type="Text"/>
    <xs:element name="long_summary" type="Text"/>
    ....