在 XSD 中,我想指定一个元素只能有空白内容 [英] In XSD I want to specify that an element can only have whitespace content

查看:19
本文介绍了在 XSD 中,我想指定一个元素只能有空白内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 XSD 应允许名为 OnlyWhiteSpaceElement 的元素具有必需的 Name 属性,并且只能包含空白内容:

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>

XSD 位于名为 Temp.XSD 的文件中,这里是由它验证的 XML.

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?

推荐答案

XSD 验证解析器实现的解释方式似乎存在一些差异:

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

<xs:whiteSpace value="collapse"/>

Xerces 将在折叠内部空白后修剪掉剩余的前导和尾随空白,但 .NETLiquid XML Studio 显然不会将这种情况崩溃到空字符串.

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.

最好完全避免这个问题并使用 xs:pattern 代替:

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

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

这是使用上述更改更新的整个 XSD:

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>

这篇关于在 XSD 中,我想指定一个元素只能有空白内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆