【问题标题】:Why is my XSD allowing a prohibited attribute in my XML?为什么我的 XSD 允许我的 XML 中的禁止属性?
【发布时间】:2023-04-08 02:42:01
【问题描述】:

我已将名为 Classroom 的属性插入到名为 Lecture 的元素中。这是 XML 架构:

<xsd:attribute name="Classroom" use="required">
<xsd:simpleType>
    <xsd:restriction base="xsd:string">
        <xsd:minLength value="0"/>
        <xsd:maxLength value="7"/>
    </xsd:restriction>
</xsd:simpleType>

.
.
.
<xsd:complexType name="labType"> 
        <xsd:complexContent>
            <xsd:restriction base="eventType">
                <xsd:sequence>
                    <xsd:element name="Title" type="xsd:string"/>
                    <xsd:element name="Lecture" maxOccurs="10" minOccurs="1"/>
                </xsd:sequence>
                        <xsd:attribute name="Clasroom" use="prohibited"/>
                </xsd:restriction>
        </xsd:complexContent>
    </xsd:complexType>
.
.
.
<xsd:element name="Lab" substitutionGroup="Event" type="labType"/>

我遇到的问题是此架构没有对属性应用限制。 我试图验证这个 XML 代码:

 <Lab>
                <Title>Artificial Intelligence</Title>
                <Lecture Classroom="BA">    
                    <Day>Friday</Day>
                    <Time>17:00-18:00</Time>
                </Lecture>                  
    </Lab>

我的问题是这个 XML 被报告为有效,即使它使用属性“教室”(它不应该这样做)。 我是 XML 新手,所以请不要苛刻。 提前谢谢!

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    你的Lecture声明,

    <xsd:element name="Lecture" maxOccurs="10" minOccurs="1"/>
    

    声明Lecture 没有类型,因此您实际上允许Lecture 上的任何内容和任何属性,无论您可能在Classroom 中的其他任何位置定义的任何限制XSD。

    这是一个完整的 XSD,它将成功验证您的 XML:

    <xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Lab">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Title" type="xs:string"/>
            <xs:element name="Lecture">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Day" type="xs:string"/>
                  <xs:element name="Time" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="Classroom">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:minLength value="0"/>
                      <xs:maxLength value="7"/>
                    </xs:restriction>
                  </xs:simpleType>              
                </xs:attribute>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    上述 XSD 将允许 Lecture 上的 Classroom 属性,但会将值限制为长度在 0 到 7 之间的字符串。

    您还可以添加xs:attribute/use="prohibited" 以防止@Classroom 出现在Lecture 上,但您的原始标题和您的这部分问题,

    我遇到的问题是此架构不适用于 属性限制。

    暗示您的问题的重点是为什么限制没有产生效果。也许您指的是一般意义上的任何限制——xs:restriction 或use="prohibit"。好吧,答案是一样的:不给 Lecture 分配类型,就是允许它是任何类型并具有任何属性。

    【讨论】:

      【解决方案2】:

      Clasroom 中的错字(只有 1 个's')

      <xsd:attribute name="Clasroom" use="prohibited"/>
      

      问题的另一部分是属性限制被应用于Lab 元素而不是Lecture 元素。

      【讨论】:

      • prohibited (+1) 很好。原标题提到了“限制”,但你可能是对的,OP 想知道为什么对一般意义上的属性的限制,包括use="prohibited",对 XML 没有影响。我认为根本原因是未能为 Lecture 指定类型。
      猜你喜欢
      • 2016-09-24
      • 2017-02-21
      • 2021-04-14
      • 2019-08-03
      • 2014-10-17
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多