0

I would like to assign an attribute to EACH element I have in my XSD.

Example:

mySchema.xsd

to each of these elements:

<xs:element name="description" type="xs:string" />
<xs:element name="benefits" type="xs:string"/>
<xs:element name="best_practices" type="xs:string"/>
<xs:element name="example" type="xs:anyURI"/>

I would like that they have this attribute:

<xs:attribute name="label" type="xs:string"/> 

In this example, the elements are only 4, but in my case there are many many elements,...any suggestion?

Thanks!

4

1 回答 1

0

如果您想添加一个属性,那么您实际上是将元素的类型xs:string从更改为您自己的complexType包含该属性的自定义。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://example.com/namespace"
           xmlns:tns="http://example.com/namespace"
           elementFormDefault="qualified">

  <xs:complexType name="labelledString">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="label" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

有了这种类型,您可以简单地使用type="tns:labelledString"而不是type="xs:string"在您的元素上使用,并且相同的方法适用于anyURI(使用另一种自定义类型 extends xs:anyURI)。

于 2013-11-06T16:20:32.447 回答