XSD:如何同时使用值和属性 [英] XSD : How to use both value and attribute

查看:24
本文介绍了XSD:如何同时使用值和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证具有字符串属性和字符串值的元素:

I'd like to validate an element having a string attribute and a string value :

<?xml version="1.0" encoding="UTF-8"?>
<name sex="M">eric</name>

我用过:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name">
  <xs:complexType>
    <xs:restriction base="xs:string"/>
    <xs:attribute name="sex" type="xs:string"/>
  </xs:complexType>
</xs:element>
</xs:schema>

它说内容无效.

文档说具有属性的元素总是complexType".如果我省略 xs:restriction 行,则内容必须为空.但我想要一个字符串值(eric").

The doc says that an element with an attribute is always "complexType". If I omit the xs:restriction line, the content must be empty. But I want a string value in it ("eric").

XSD 代码应该是什么?

What should be the XSD code ?

PS:我想避免丑陋的 'mixed="true"'

PS: I'd like to avoid the ugly 'mixed="true"'

推荐答案

您需要具有简单内容和属性的复杂类型.

You need complex type with simple content and an attribute.

<xs:element name="name">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="sex" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

这篇关于XSD:如何同时使用值和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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