此xml的架构应该是什么?

好奇的

我正在尝试为以下XML示例编写XSD架构:

<?xml version="1.0" encoding="UTF-8"?>
<locs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="loc.xsd">
    <loc required="true"  comment="A comment">ABC</loc>
</locs>

我试图在架构中强制执行以下规则的规则:

  1. 根元素locs必须具有1个或多个元素loc
  2. loc必须具有2个属性:requiredbooleancommentstring非零长度的,不是完全由空格和/或标点符号组成
  3. loc 必须具有非零长度的字符串值,并且不完全由空格和/或标点符号组成

我已经做到了loc.xsd以下几点:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="locs">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="loc" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="required" type="xs:boolean" use="required"/>
                                <xs:attribute name="comment" type="xs:string" use="required"/>
                            </xs:extension>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这样我就可以强制执行规则1和2,但是第3条规则没有得到执行,因此如下所示的输入项是有效的:

<loc required="false"  comment="Another comment"/>

我想念什么?我已经花了几个小时了!

CM Sperberg-McQueen

你很亲密

您说第三个约束没有被强制执行,但是您显示的示例并未说明非强制执行:元素

<loc required="false"  comment="Another comment"/>

内容长度为零。当您说loc的类型是xs:string的扩展时,您告诉XSD验证程序这没关系。

JirkaŠ。的答案说明了您需要采取的方法。如果您对任何非空字符串感到满意,则可以逐字采用该解决方案。但是在您这样做之前,请确保您愿意接受这样的实例:

<loc required="false"  comment="&#x9; ">  </loc>

如果不是,那么您的要求不是注释和位置应该具有字符串值,也不是它们应该具有非空字符串值,而是更严格的值。当然,理想情况下,您希望它们具有有用的有意义的值,但是可能无法正式定义有用的注释集或有用的loc值集。有人满足于说他们想要一个不完全由空格和标点符号组成的非空字符串(因此它至少具有一个与该类匹配的字符\w)。

<xs:simpleType name="nonEmptyNonWSString">
  <xs:restriction base="xs:string">
    <xs:minLength value="1" />
    <xs:pattern value="(\W)*\w(\W|\w)*"/>
  </xs:restriction>
</xs:simpleType>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章