简体   繁体   English

使用 xsd 验证元素和属性

[英]Validate a element and an attribute with xsd

<spokenLanguages>
        <language fluency="3">German</language>
        <language fluency="3">English</language>
        <language fluency="1">Spanish</language>
    </spokenLanguages>

I am trying to validate this xml document so that I can have at least a minimal of 2 language and a maximum of unbounded.我正在尝试验证此 xml 文档,以便我至少可以拥有 2 种语言和最多无界语言。 I tried using this code我尝试使用此代码

<xsd:complexType name="LanguageType">
<xsd:choice minOccurs="2">
    <xsd:element name="language" type="xsd:string" minOccurs="2" maxOccurs="unbounded">     
        <xsd:attribute name="fluency" use="required">
                <xsd:restriction base="xsd:integer">
                    <xsd:enumeration value="1"/>
                    <xsd:enumeration value="2"/>
                    <xsd:enumeration value="3"/>
                    <xsd:enumeration value="4"/>
                </xsd:restriction>
        </xsd:attribute>    
    </xsd:element>
</xsd:choice>

but it isn't validating.但它没有验证。 Any suggestions??有什么建议??

You had several problems with your XSD schema, including that you never defined an <xsd:element> for the spokenLanguages tag.您的 XSD 架构有几个问题,包括您从未为spokenLanguages标记定义<xsd:element> Also, the fluency attribute should be defined as a simple type.此外,fluency 属性应定义为简单类型。

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
    version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="spokenLanguages">
        <xsd:complexType>
            <xsd:sequence minOccurs="2" maxOccurs="unbounded">
                <xsd:element name="language">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:integer">
                            <xsd:enumeration value="1"/>
                            <xsd:enumeration value="2"/>
                            <xsd:enumeration value="3"/>
                            <xsd:enumeration value="4"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM