开发者

Can we add new attribute or change type of existing attribute to a "Referenced Element"?

In my XML schema I have an element being referenced tens of times by other elements but with different enumerated values for one of its attribute.

For now, instead of creating this element in global space and referencing it later, I am creating a new instance wherever it is needed. This approach has increased my schema size enormously because of repeated creation of almost same element many times. It also may have adverse effect on efficiency of the schema.

The only way that I see is to create element once and then reference it many times but my problem is: one of the attribute of this referenced element is required to have a different set of enumerations for each referencing element.

My question is:

Is it possible to to add an attribute to a "Referenced Element" in XML Schema?

Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myDomain.com" xmlns="http://www.myDomain.com" elementFormDefault="qualified">

  <xs:simpleType name="myValues1">
    <xs:restriction base="xs:string">
      <xs:enumeration value="value1" />
      <xs:enumeration value="value2" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="myElement">
    <xs:complexType mixed="true">
      <xs:attribute name="attr1" type="xs:string" />
      <xs:attribute name="attr2" type="xs:string" />
    </xs:complexType>
  </xs:element>

  <xs:element name="MainElement1">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="myElement">
          <xs:complexType>
            <xs:attribute name="myAtt" type="myValues1" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="mainAtt1" />
    </xs:complexType>
  </xs:element>

</xs:schema>

Or can we change type of an existing attribute of a "Referenced Element" in XML Schema?

something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myDomain.com" xmlns="http://www.myDomain.com" elementFormDefault="qualified">

  <xs:simpleType name="myValues1">
    <xs:restriction base="xs:string">
      <xs:enumeration value="value1" />
      <xs:enumeration value="value2" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="myValues2">
    <xs:restriction base="xs:string">
      <xs:enumeration value="value3" />
      <xs:enumeration value="value4" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="myElement">
    <xs:complexType mixed="true">
      <xs:attribute name="attr1" type="xs:string" />
      <xs:attribute name="attr2" type="xs:string" />
      <xs:attribute name="myAtt" type="myValues1" />
    </xs:complexType>
  </xs:element>

  <xs:element name="MainElement1">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="myElement">
          <xs:complexType>
      开发者_开发问答      <xs:attribute name="myAtt" type="myValues2" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="mainAtt1" />
    </xs:complexType>
  </xs:element>

</xs:schema>


You cannot override the content model of a referenced element. The point of the reference is that it points to exactly the same element every time.

If you really want the element to have different content, you are better off defining multiple global complex types and using them, rather than using an element reference:

<xs:complexType name="Type1" mixed="true">
   <xs:attribute name="attr1" type="xs:string" />
   <xs:attribute name="attr2" type="xs:string" />
   <xs:attribute name="myAtt" type="myValues1" />
</xs:complexType>

<xs:complexType name="Type2" mixed="true">
   <xs:complexContent>
      <xs:extension base="Type1">
         <xs:attribute name="myAtt2" type="myValues2" />
      </xs:extension>
   </xs:complexContent>
</xs:complexType>

<xs:element name="MainElement1">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="myElement" type="Type1"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

I would not worry too much about performance. The reference to the global types will be resolved only once at schema load time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜