【问题标题】:Validating XML using schema, attribute in type elements [duplicate]使用模式验证 XML,类型元素中的属性 [重复]
【发布时间】:2018-01-28 11:43:00
【问题描述】:

我正在尝试使用架构验证以下 XML。

<?xml version="1.0" encoding="ISO-8859-1"?>
<Noticias xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="UXMLS-5.xsd">
  <Noticia dia="12" mes="3" año="2004" seccion="Nacional">
    <medio tipo="radio">Onda Cero</medio>
    <periodista>Fermín Bocos</periodista>
    <titular>Atentado bestial en Madrid</titular>
    <resumen>Resumen de la noticia Atentado bestial en Madrid</resumen>
  </Noticia>
</Noticias>

我在验证上面的下面一行时被卡住了:

<medio tipo="radio">Onda Cero</medio>

我能够验证具有属性的“Noticia”,因为它不是具有关联类型的最终元素,但当它是具有关联内容的子元素时我不能。

我尝试了很多方法,到目前为止没有运气,我也无法在网上找到解决方案。有任何想法吗?是我遗漏了什么还是无法做到?

更新: 这是目前为止的 XSD,请记住“medio”位是错误的。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Noticias">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Noticia" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="medio" type="xs:string">
                <xs:attribute name="tipo" type="xs:string">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="radio"/>
                      <xs:enumeration value="prensa"/>
                      <xs:enumeration value="television"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:element>
              <xs:element name="periodista" type="xs:string"/>
              <xs:element name="titular" type="xs:string"/>
              <xs:element name="resumen">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[a-zA-Z0-9]{60}"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="dia" type="dia"/>
            <xs:attribute name="mes" type="mes"/>
            <xs:attribute name="año" type="xs:integer"/>
            <xs:attribute name="seccion" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="dia">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="12"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="mes">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="31"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

【问题讨论】:

  • 到目前为止,您的 XSD 看起来如何?
  • 我更新了主帖。
  • 您试图定义的东西被称为“具有简单内容的复杂类型”:知道这应该有助于您找到正确的信息。

标签: xml validation attributes schema xsd-validation


【解决方案1】:

您的验证标记有两个问题:

  1. 您的xs:attribute 周围缺少xs:complexTypexs:simpleContent 元素。还有一个&lt;xs:extension base="xs:string"&gt;,用于指定元素内容的值类型,如explained here at W3Schools

所以medio 代码应该是这样的:

<xs:element name="medio">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="tipo">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="radio"/>
              <xs:enumeration value="prensa"/>
              <xs:enumeration value="television"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
  1. 另一个问题(不是您的问题的一部分):您的 xs:pattern 应该是 1 到 60 个字符,而不是正好 60 个字符并包含空格:

    <xs:pattern value="[\sa-zA-Z0-9]{1,60}"/>
    

现在您的 XSD 应该可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2023-03-04
    • 1970-01-01
    • 2016-02-13
    • 2018-02-09
    • 1970-01-01
    相关资源
    最近更新 更多