【问题标题】:Schema XSD: Element Unique inside a node, not repeated valuesSchema XSD:节点内的元素唯一,不重复值
【发布时间】:2011-09-29 09:59:26
【问题描述】:

我想验证一个 XML 文件,该文件具有相同节点中不能重复的值的模式。签名的顺序很重要。我的无效 XML 是:

   <person>
      <name>Peter Petrelli</name>
      <subject1>Mathematics</subject1>
      <note1>8.5</note1>
      <subject2>Natural Science</subject2>
      <note2>4.5</note2>
      <subject3>Mathematics</subject3>
      <note3>7</note3>
   </person>
</school>

数学不可能不止一次。

如何使用 XSD 架构验证这一点?

非常感谢

【问题讨论】:

    标签: xml xsd unique validation field


    【解决方案1】:

    如果设计更合理,就没有问题:

    <person>
    <name>Peter Petrelli</name>
    <subject>
        <name>Mathematics</name>
        <note>2</note>
    </subject>
    <subject>
        <name>Natural Sciences</name>
        <note>2.5</note>
    </subject>
    <subject>
        <name>Mathematics</name>
        <note>4</note>
    </subject>
    

    那么 元素内的 定义可以是:

        <unique name="su">
            <selector xpath="subject" />
            <field xpath="name"/>
        </unique>
    

    【讨论】:

    • 确实如此。您使用包含后缀 -1、-2、-3 等的元素名称似乎使得编写合理的 XSD 模式几乎是不可能的。它还将使 XML 的任何处理(例如使用 XSLT 或 XPath)变得更加困难。这是一个非常糟糕的设计。
    • 我只写了一个小例子,但我已经按照我们的建议更改了我的设计,从现在开始我可以正确验证。非常感谢你们,伙计们
    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多