【问题标题】:Validate XML Element Attribute value using XSD使用 XSD 验证 XML 元素属性值
【发布时间】:2014-01-17 10:26:07
【问题描述】:

我有以下 XML 元素。 我需要验证 Parameter 属性在以下 XML 元素中是否仅包含 Y 或 N

<Test Script="abc.sh" Parameter="Y"/>
               **OR**
<Test Script="abc.sh" Parameter="N"/>

我的 XSD 是:

    <xs:element name="Test" minOccurs="0">
        <xs:complexType>
            <xs:attribute name="Script" type="xs:string" use="required"/>
            <xs:attribute name="Parameter" type="xs:string" use="optional"/>                    
        </xs:complexType>
    </xs:element>

目前,此 XSD 无法验证参数是持有 Y 还是 N

【问题讨论】:

    标签: xml xslt xml-parsing xsd


    【解决方案1】:

    您需要使用代表restriction 的simpleType 来定义attribute,以强制属性值是一组已定义值的成员。

    假设您有以下 xml:

    <?xml version="1.0"?>
    <Test Script="path/to/script" Parameter="Y" xmlns="http://www.example.org" />
    

    您可以使用此 xsd 强制他们的 type 属性具有 foo 或 bar 的值:

    <?xml version="1.0"?>
    <xsd:schema 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:tns="http://www.example.org"
      targetNamespace="http://www.example.org"
      elementFormDefault="qualified"
      attributeFormDefault="unqualified"
    >
        <xsd:element name="Test">
          <xsd:complexType>
            <xsd:attribute name="Script" type="xsd:string" use="required"/>
            <!-- the following has no type attribute. It's type is 
                 defined in the simpleType child -->
            <xsd:attribute name="Parameter" use="optional">
              <xsd:simpleType>
                <!-- define a set of xsd:strings
                     as possible values -->
                <xsd:restriction base="xsd:string">
                  <xsd:enumeration value="Y"/>
                  <xsd:enumeration value="N"/>
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:attribute>
          </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    

    【讨论】:

    • 我用过 ;你给了 有什么区别?
    • @logan 只是个人喜好。如果你愿意,你可以用你的xs 替换我的xsd。您使用的前缀是在架构头部的xmlns:xsd=.. 或xmlns:xs 中定义的
    • 现在我只使用 xs 但它说 org.xml.sax.SAXParseException: The prefix "xsd" for element "xsd:simpleType" is not bound.
    • 在这种情况下没关系。保留之前的值,或者直接删除它(使用默认值)。如果它仍然因为 xsd|xs 命名问题而抱怨,请确保您只使用其中之一。
    • 这取决于您的验证器,我使用的是xmllint,它说:` Element '{example.org}Test', attribute 'Parameter': [facet 'enumeration'] The value ' foo' 不是集合 {'Y', 'N'} 的元素。`
    【解决方案2】:

    你必须像这样定义你的参数属性

          <xs:simpleType name="yesno">
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Y" />
                    <xs:enumeration value="N" />
                </xs:restriction>
          </xs:simpleType>
    
    
    
          <xs:attribute name="Parameter" type="xs:yesno" use="optional"/>
    

    【讨论】:

    • 无效参数的错误消息说“原因:cvc-enumeration-valid:值'YY'对于枚举'[Y,N]'不是构面有效。”这不可能在错误消息中显示 XML 属性名称?
    • 我不确定您是如何解析 xml 文档的。您可能需要查看 API 文档(如果以编程方式解析)或用户指南(如果使用任何工具)。
    【解决方案3】:

    试试这个:

    <xs:element name="Test" maxOccurs="unbounded">
                    <xs:complexType>
                                <xs:attribute name="Script">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="abc.sh">
                                            </xs:enumeration>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                  <xs:attribute name="Parameter">
                                     <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:pattern value="Y|N" >
                                            </xs:pattern>
                                        </xs:restriction>
                                     </xs:simpleType>    
                                  </xs:attribute>
                    </xs:complexType>
    </xs:element>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      相关资源
      最近更新 更多