微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSD 以及在哪里将模式应用于具有属性的子标记

如何解决XSD 以及在哪里将模式应用于具有属性的子标记

XML 根据 XSD 进行验证。现在,我在何处/如何向 B 标记添加如下所示的模式标记时遇到了问题:

<xs:pattern value="(?i)[a-z0-9]+@[a-z0-9]+\.[a-z0-9]{3}"/>

我尝试使用标签属性的扩展与限制链接在一起,但它只是不起作用。对于这种情况的示例和解决方案,我已经四处寻找,但没有找到任何东西。我显然在这里遗漏了一些东西。

我可以在哪里设置限制以验证 <B> 标记内容

我的示例 XML 是这样的:

<ImportA>
    <A>
        <Id>A1</Id>
        <Bs>
            <B priority="true">a@b.com</B>
            <B>y@z.com</B>
        </Bs>
    </A>
</ImportA>

我有以下 XSD:

<xs:element name="ImportA">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="A" type="AType" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="AType">
    <xs:sequence>
        <xs:element name="Id" type="xs:string" minOccurs="1" maxOccurs="1"/>
        <xs:element name="Bs" minOccurs="0" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="B" type="BType" minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="priority" type="PriorityOrEmptyType"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

<xs:simpleType name="Priority">
    <xs:restriction base="xs:string">
        <xs:pattern value="(?i)true"/>
        <xs:pattern value="(?i)false"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="PriorityOrEmptyType">
    <xs:union memberTypes="Priority EmptyStringType"/>
</xs:simpleType>

<xs:simpleType name="EmptyStringType">
    <xs:restriction base="xs:string">
        <xs:enumeration value=""/>
    </xs:restriction>
</xs:simpleType>

任何帮助将不胜感激。谢谢!

解决方法

我尝试使用标签将属性的扩展与限制链接在一起

您不能扩展/限制属性或标签。您只能扩展/限制类型定义。

但它只是不工作

理想情况下,您应该告诉我们您尝试过的内容 - 这样,我们就会知道您尝试过的内容,以及您的理解略有偏差的地方。

我可以在哪里设置限制来验证标签的内容?

实际上,这比您想象的要容易。您将它放在与普通标签相同的位置 - 在标签的简单类型定义上。在这种情况下,描述标签的文本值的类型定义是您扩展的简单类型。所以...

代替

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
        ...

试试:

<xs:simpleType name="EmailType">
    <xs:restriction base="xs:string">
        <xs:pattern value="(?i)[a-z0-9]+@[a-z0-9]+\.[a-z0-9]{3}"/>
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="BType">
    <xs:simpleContent>
        <xs:extension base="EmailType">
       ...

(未测试,但类似的东西应该可以完成工作)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。