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

泄露秘密 提交于 2019-12-24 11:37:30

问题


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?


回答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>


来源:https://stackoverflow.com/questions/52280613/in-xsd-i-want-to-specify-that-an-element-can-only-have-whitespace-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!