In XSD I want to specify that an element can only have whitespace content
Asked Answered
S

1

2

The following XSD should allow for an element called OnlyWhiteSpaceElement which has a required Name attribute, and can have only whitespace content:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="OnlyWhiteSpaceType">
        <xs:simpleContent>
            <xs:extension base="OnlyWhiteSpaceContentType">
                <xs:attribute name="Name" type="xs:string" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:simpleType name="OnlyWhiteSpaceContentType">
        <xs:restriction base="xs:string">
            <xs:whiteSpace value="collapse"/>
            <xs:length value="0"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="MyType">
        <xs:sequence>
            <xs:element name="OnlyWhiteSpaceElement" type="OnlyWhiteSpaceType"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="MyXml" type="MyType">
    </xs:element>
</xs:schema>

The XSD is in a file called Temp.XSD, and here is the XML that is validated by it.

<?xml version="1.0" encoding="utf-8"?>
<MyXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Temp.xsd">
    <OnlyWhiteSpaceElement Name="MyOnlyWhiteSpaceElement">

    </OnlyWhiteSpaceElement>
</MyXml>

However the validation fails. The end tag shows an error that reads

The 'OnlyWhiteSpaceElement' element is invalid - the value '     ' is invalid according to its datatype 'OnlyWhiteSpaceType' - the actual length is not equal to the specified length.

Can anyone see what I'm doing wrong?

Sunbow answered 11/9, 2018 at 16:43 Comment(0)
W
1

There appears to be some differences in how XSD validating parser implementations interpret:

<xs:whiteSpace value="collapse"/>

Xerces will prune away leading and trailing whitespace remaining after collapsing internal whitespace, but the XSD validators in .NET and Liquid XML Studio apparently do not collapse such cases to the empty string.

It's probably better to avoid the issue altogether and use xs:pattern instead:

  <xs:pattern value="\s*"/>

Here's the entire XSD updated with the above change:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="OnlyWhiteSpaceType">
    <xs:simpleContent>
      <xs:extension base="OnlyWhiteSpaceContentType">
        <xs:attribute name="Name" type="xs:string" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="OnlyWhiteSpaceContentType">
    <xs:restriction base="xs:string">
      <xs:pattern value="\s*"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="MyType">
    <xs:sequence>
      <xs:element name="OnlyWhiteSpaceElement" type="OnlyWhiteSpaceType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="MyXml" type="MyType">
  </xs:element>
</xs:schema>
Whyalla answered 11/9, 2018 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.