English 中文(简体)
XSD 校验 XSD XML 的属性为真
原标题:XSD Validation XML has an Attribute true

我的问题是,如果我必须验证 只有一个人的属性是真实的, 而其中之一必须是真实的, 使用xml:

<addresses>
    <address>
        <primary>true</primary>
        <street>One str.</street>
        <number>111</number>
    </address>
    <address>
        <primary>false</primary>
        <street>Two str.</street>
        <number>222</number>
    </address>
</addresses>

怎么办?

Ps:对不起我的英语。

最佳回答

最简单的方式是使用略微不同的 XML 结构。 您想要一个主地址和零或更多的非主地址。 任何 XML 验证方法( DTDs, XSD,...) 都很容易说, 如果您对有不同要求的东西使用不同的名称。 如果 XML 结构变成这样, 则使用不同的名称 。

<addresses>
  <primary-address>
    <street>One str.</street>
    <number>111</number>
  </primary-address>
  <address>
    <street>Two str.</street>
    <number>222</number>
  </address>
</addresses>

然后很容易写出 XSD 模式,它限制 < code> addresses 精确包含一个主地址,并明确显示主地址和其他地址的结构相同:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"> 

  <xsd:complexType name="address">
    <xsd:sequence>
      <xsd:element ref="street"/>
      <xsd:element ref="number"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="primary-address" type="address"/>  
  <xsd:element name="address" type="address"/>  
  <xsd:element name="addresses">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="primary-address"/>
        <xsd:element ref="address" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="mixed" mixed="true">
    <xsd:sequence/>
  </xsd:complexType>

  <xsd:element name="street" type="mixed"/>
  <xsd:element name="number" type="mixed"/>
</xsd:schema>

如果您不能改变您的 XML 结构, 或者不想改变, 您可以切换到 XSD 1. 1. 1 并使用声明来强制执行限制, 或者您可以使用Schematron来这样做 。

或者(这是一个略为肮脏的把戏),你可以说 (1), broad 属性的唯一法律价值是 true , (2) 该属性在 mail 元素上是可选的, (3) 每一次出现 big 属性都必须有一个独特的价值 。

但我真的建议改变XML结构

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签