xmlxsd

How to xsd validate formatted text with length restriction


I fail to create a xsd file to validate a comment element.

  1. The element "comment" can contain text and optional children <b>, <i>, <ul>
  2. The children elements can be nested and appear multiple times
  3. The content of comment has to have a length restriction (60)

I was able to fullfill 1. and 2. but how can I add the length restriction?

Thank you so much for your help! Andreas

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Test">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="comment" type="FormattedText" minOccurs="0" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  <xs:complexType name="FormattedText" mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="b" minOccurs="0" />
      <xs:element ref="i" minOccurs="0" />
      <xs:element ref="ul" minOccurs="0" />
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="ULType">
    <xs:sequence>
      <xs:element 
        name="li" 
        minOccurs="1" 
        maxOccurs="unbounded">
        
        <xs:complexType mixed="true">
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="b" minOccurs="0" />
            <xs:element ref="i" minOccurs="0" />
            <xs:element ref="ul" minOccurs="0" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  
  <xs:element name="b" type="FormattedText" />
  <xs:element name="i" type="FormattedText" />
  <xs:element name="ul" type="ULType" />
</xs:schema>

This is an example for a xml:

<?xml version="1.0" encoding="UTF-8"?>
<Test>
  <comment>
    Abstellorte:
    <ul>
      <li>Hinter <b>To<i>r</i></b></li>
      <li>Am <i>Fenster</i></li>
    </ul>
  </comment>
</Test>

Solution

  • You can do this in XSD 1.1 with an assertion: test="string-length(.) le 60". It can't be done in XSD 1.0.