定义具有属性和可选值的元素 [英] Define an element with attribute and optional value

查看:30
本文介绍了定义具有属性和可选值的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建可以验证以下两个示例之一的 xsd?

How do I create an xsd that can validate either of the following two examples?

<element attribute="attribute-value" />

<element attribute="attribute-value">element-value</element>

推荐答案

  <?xml version="1.0" encoding="utf-8"?>
  <xs:schema id="test"
      targetNamespace="http://tempuri.org/test.xsd"
      elementFormDefault="qualified"
      xmlns="http://tempuri.org/test.xsd"
      xmlns:mstns="http://tempuri.org/test.xsd"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
  >
    <xs:complexType name="element_type">
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="attribute" type="xs:string" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>

    <xs:element name="root">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="element" type="element_type">
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:schema>

这假定该属性不是必需的.

This assumes that the attribute isn't required.

这篇关于定义具有属性和可选值的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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