XML 架构:具有仅包含文本的属性的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:15:51  来源:igfitidea点击:

XML Schema: Element with attributes containing only text?

xmlxsd

提问by Wilco

I'm having difficulty searching for this. How would I define an element in an XML schema file for XML that looks like this:

我很难找到这个。我将如何在 XML 架构文件中为 XML 定义如下所示的元素:

<option value="test">sometext</option>

I can't figure out how to define an element that is of type xs:stringand also has an attribute.

我不知道如何定义一个有类型xs:string并且也有属性的元素。

Here's what I've got so far:

这是我到目前为止所得到的:

<xs:element name="option">
    <xs:complexType>
        <xs:attribute name="value" type="xs:string" />
    </xs:complexType>
</xs:element>

回答by David Norman

Try

尝试

  <xs:element name="option" type="AttrElement" />

  <xs:complexType name="AttrElement">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="value" type="xs:string">
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

回答by Julian H

... or the inline equivalent:

...或内联等效项:

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

回答by Aitor

I know it is not the same, but it works for me:

我知道它不一样,但它对我有用:

<xsd:element name="option">
    <xsd:complexType mixed="true">
        <xsd:attribute name="value" use="optional" type="xsd:string"/>
    </xsd:complexType>
</xsd:element>