如何验证此 xml 输入? [英] How to validate this xml input?

查看:21
本文介绍了如何验证此 xml 输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XML 新手,需要帮助.我有这个 XML 代码,我必须为其创建一个架构验证器:

I'm new to XML and I need help. I have this XML code for which I have to make a schema validator:

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
    <family pol="m">Peter</family>
    <age>40</age>
    <email>ppenzov@ii.com</email>
</user>
<user>
    <family pol="w">Penz</family>
    <age>65</age>
</user>
<user>
    <family pol="w">Penzov</family>
    <age>19</age>
    <email>pppenzov@ii.com</email>
</user>
</users>

xml 方案必须遵循以下规则:

The xml scheme must follow these rules:

家庭、年龄和电子邮件必须严格有序

family - 必须出现一次并且最多有 20 个符号

family - must occur once and have at most 20 symbols

age - 必须出现一次并且是 1 到 100 之间的数字

age - must occur once and be a number between 1 and 100

email - 最多只能出现一次,但可以省略;它应该是一个介于 5 到 10 个符号之间的字符串

email - must occur at most once, but may be omitted; it should be a string between 5 and 10 symbols

你能帮我创建一个非常基本的例子吗?

Would you help me please to create a very basic example?

到目前为止,我已经这样做了:

So far I have done this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3schools.com"
           xmlns="http://www.w3schools.com"
           elementFormDefault="qualified">

<xs:element name="user">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="family" type="xs:string">
        <xs:restriction base="xs:string">
          <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
          <xs:enumeration value="family" maxOccurs="1"/>
          <xs:length value="20"/>
        </xs:restriction>
      </xs:element name="family" type="xs:string">
      <xs:element name="age" type="xs:intreger">
        <xs:restriction base="xs:integer">
          <xs:minInclusive value="1"/>
            <xs:maxInclusive value="100"/>
            <xs:enumeration value="age" maxOccurs="1"/>
        </xs:restriction>
      </xs:element name="age" type="xs:intreger">
      <xs:element name="email" type="xs:string">
        <xs:restriction base="xs:string">
          <xs:minLength value="5"/>
          <xs:maxLength value="10"/>
          <xs:enumeration value="email" maxOccurs="1"/>
        </xs:restriction>
      </xs:element name="email" type="xs:string">
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

推荐答案

您已经接近了,但是当前形式的架构存在许多问题.

You're close, but there are a number of issues with the schema in its current form.

首先,它不是 XML.在 XML 中,属性在元素的开始标记上指定,并且仅在开始标记上指定.因此,无论您在哪里遇到这样的事情,都需要做出改变:

First, it's not XML. In XML, attributes are specified on the start-tag, and only on the start-tag, of an element. So wherever you have things like this, they need to change:

<xs:element name="family" type="xs:string">
  ...
</xs:element name="family" type="xs:string">

正确的形式是

<xs:element name="family" type="xs:string">
  ...
</xs:element>

如果这是复制/粘贴错误,您只需要更加小心.如果这是一个概念问题,您需要阅读 XML 的基础知识.格式良好的规则很简单,但 XML 处理器不能容忍违规.(事实上​​,规范禁止它们容忍违规行为.)

If this is a copy/paste error, you just need to be more careful. If it's a conceptual issue, you need to read up on the basics of XML. The rules of well-formedness are simple, but XML processors do not tolerate violations. (They are in fact forbidden by the spec to tolerate violations.)

您会发现使用支持 XML 的编辑器很有帮助,它会自动提醒您注意此类格式良好的问题.(如果您已经在使用它,您应该更加注意它的遇险信号.)

You will find it helpful to work with an XML-aware editor, which will alert you automatically to well-formedness issues like this. (If you're already using one, you should work to become more attentive to its signals of distress.)

其次,在清除格式良好的问题后,您的架构文档对于架构文档的 XSD 架构无效.有几个问题.

Second, after cleaning up the well-formedness issues, your schema document is not valid against the XSD schema for schema documents. There are several problems.

  • 将元素绑定到特定类型的方法是使用 either type 属性(将元素绑定到命名类型)或者作为子类型的类型声明(声明一个匿名本地类型并将元素绑定到该匿名类型).不要同时使用两者:您同时告诉模式验证器您想要将元素 age(例如)绑定到类型 xs:integer 并且您想要将它绑定到不同的类型,通过限制从 xs:integer 派生.

  • The way to bind an element to a particular type is to use either the type attribute (to bind the element to a named type) or a type declaration as a child (to declare an anonymous local type and bind the element to that anonymous type). Don't use both: you're simultaneously telling the schema validator that you want to bind the element age (for example) to the type xs:integer and that you want to bind it to a different type, derived from xs:integer by restriction.

声明类型的方法是使用 xs:complexType 或 xs:simpleType 元素,而不是 xs:restriction.你想要的结构是这样的:

The way to declare a type is to use the xs:complexType or the xs:simpleType element, not xs:restriction. The structure you want is something like this:

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      ...
    </xs:restriction>
  </xs:simpleType>
</xs:element>

很难记住 XSD 模式文档中的内容并不罕见;许多人发现使用支持 XSD 的编辑器是值得的.

It's not unusual find it hard to remember what goes where in an XSD schema document; many people find it pays off to work with an XSD-aware editor.

xs:enumeration 元素没有名为 maxOccurs 的属性,并且猜测它并不意味着您认为它意味着什么.您似乎在推测 xs:enumerate 用于指定元素在给定内容模型中可以出现的次数;这是由 xs:element 元素上的 minOccurs 和 maxOccurs 属性完成的.

The xs:enumeration element does not have an attribute named maxOccurs, and at a guess it doesn't mean what you think it means. You seem to be speculating that xs:enumerate is used to specify how many times an element can occur in a given content model; that's done by the minOccurs and maxOccurs attributes on the xs:element element.

xs:enumerate 元素用于枚举一个类型的值空间中可能的值;<xs:enumeration value="family"/> 意味着例如正在定义的类型的合法值之一是字符串family".如果它是类型声明中唯一的 xs:enumeration 元素,则意味着family"是类型的唯一有效值.

The xs:enumerate element is used to enumerate the possible values in the value space of a type; <xs:enumeration value="family"/> means for example that one of the legal values of the type being defined is the string "family". If it's the only xs:enumeration element in the type declaration, it means that "family" is the only valid value of the type.

maxOccurs 属性属于 xs:element 元素;在这种情况下,由于1"是 maxOccurs 的默认值,因此可以在所有三个 xs:element 元素上省略它.您可以并且应该删除架构文档中的所有 xs:enumeration 元素.

The maxOccurs attribute belongs on the xs:element element; in this case, since "1" is the default value of maxOccurs, it can be omitted on all three xs:element elements. You can and should drop all the xs:enumeration elements in your schema document.

一旦您清除了这些错误,您的架构文档就是合法的.但这还不是你想要的意思.同样,有几个问题:

Once you have those errors cleared up, your schema document is legal. But it doesn't yet mean what you want it to mean. Again, there are several problems:

  • 您通过说(除其他外)来定义族元素的简单类型:

  • You define the simple type for the family element by saying (among other things):

<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>

这意味着 family 的值必须由拉丁字母表中的三个字母组成,没有变音符号.如果您希望值Peter"、Penz"和Penzov"合法,则不行;您需要阅读一些正则表达式及其含义,以了解如何更可靠地编写它们.

This means that the value of family must consist of exactly three letters in the Latin alphabet, without diacritics. If you want the values "Peter", "Penz", and "Penzov" to be legal, this won't do; you need to read up a bit on regular expressions and what they mean to understand how to write them more reliably.

许多名称包含带有变音符号的字符(考虑波兰语逻辑学家 Jan Łukasiewicz,波兰符号的发明者);许多包含非字母字符(空白、连字符、撇号).因此,将家庭"元素限制为从 A 到 Z 的字母是糟糕的数据建模,即使您只想处理拉丁字母中的名称.

Many names contain characters with diacritics (consider the Polish logician Jan Łukasiewicz, the inventor of Polish notation); many contain non-alphabetic characters (blank, hyphen, apostrophe). So restricting your 'family' element to the letters from A to Z is poor data modeling, even if you only want to deal with names in the Latin alphabet.

您正在使用 xs:length 元素尝试定义族元素的最大长度;为此,您需要 xs:maxLength,而不是 xs:length.

You are using the xs:length element to try to define the maximum length of your family element; you want xs:maxLength, not xs:length, for that.

祝你好运!

这篇关于如何验证此 xml 输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆