2

我有这个代码:

<desc> 
    <paragraphe>bala bla bla</paragraphe>
    <paragraphe>bala bla bla bla</paragraphe>
    <paragraphe>bala bla bla</paragraphe>
</desc>

我想限制desc元素的最大长度为 120 个字符,paragraphe仅包括所有内容。

例如对于单个paragraphe元素,我可以对 40 个字符的固定最大长度执行此操作:

<xs:element name="paragraphe">
   <xs:simpleType>
       <xs:restriction base="xs:string">
           <xs:maxLength value="40"/>
       </xs:restriction>
   </xs:simpleType>
</xs:element>

desc但我想将(总paragraphe字符)的长度限制为 120 个字符。

4

1 回答 1

4

您可以xs:assert在 XSD 1.1 中使用:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
           elementFormDefault="qualified"
           vc:minVersion="1.1">
    <xs:element name="desc">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="paragraphe" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:assert test="string-length(string-join(paragraphe, '')) &lt; 12"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

那么这个 XML 文档将是有效的:

<desc>
    <paragraphe>asdf</paragraphe>
    <paragraphe>asdf</paragraphe>
    <paragraphe>asd</paragraphe>
</desc>

此 XML 文档将无效

<desc>
    <paragraphe>asdf</paragraphe>
    <paragraphe>asdf</paragraphe>
    <paragraphe>asdf</paragraphe>
</desc>

(只需在测试后更改为12120

于 2015-03-09T16:26:49.897 回答