开发者

xml schema attribute ref

i have this xml schema

 <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://hidden/abc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="http://hidden/abc" elementFormDefault="qualified"
     attributeFormDefault="unqualified" version="1.8">

<xs:element name="inv_constraint">
      <xs:complexType>
       <xs:sequence>
---lots of stuff---
       </xs:sequence>
       <xs:attribute name="unaryOperator"> 
        <xs:annotation>
         <xs:documentation>Negate an entire expression.</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:enumeration value="not"></xs:enumeration>
          <xs:enumeration value="-"></xs:enumeration>
         </xs:restriction>
        </xs:simpleType>
       </xs:attribute>
      </xs:complexType>
     </xs:element>

and then this xml file that uses it:

<?xml version="1.0" encoding="UTF-8"?>
<OCL xmlns="http://hidden/abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://hidden/abc abc.XSD">
------ lots of stuff
     <inv_constraint unaryOperator="not">
                <property src="A1" ref="PR1"/>
                <matOperation operator="ge">
                    <value>0</value>
                </matOperation>
            </inv_constraint>

if i change the xml schema to use the attribute with ref="" like this:

...
<xs:attribute ref="unaryOperator"></xs:attribute>
  </xs:complexType>
 </xs:element>


<xs:attribute name="unaryOperator">
 <xs:annotation>
  <xs:documentation>Negate an entire expression.</xs:documentation>
 </xs:annotation>
 <xs:simpleType>
  <xs:restriction base="xs:string">
   <xs:enumeration value="not"></xs:enumeration>
   <xs:enumeration value="-"></xs:enumeration>
  </xs:restriction>
 </x开发者_开发知识库s:simpleType>

then my xml becomes:

<inv_constraint xmlns:ns1="http://hidden/abc" ns1:unaryOperator="not">

but i want to use the ref and have my xml like

    <inv_constraint unaryOperator="not">

How can i do it? thank you


In XML Schemas all global element, attribute or type definitions must be qualified. Thus all globally defined attributes will be namespace prefixed even if you have defined attributeFormDefault as "unqualified". Workaround is to define this attribute inside a attribute group or a global type and then you can reference to this named group or to extend this named type.

<xs:attributeGroup name="unaryGroup">
    <xs:attribute name="unaryOperator"> 
        <xs:annotation>
            <xs:documentation>Negate an entire expression.</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="not"></xs:enumeration>
                <xs:enumeration value="-"></xs:enumeration>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:attributeGroup>

When you need this attribute, refer to the attributeGroup instead of the attribute. Attribute group doesn't need to contain all attributes the element uses, this should also be valid:

<xs:complexType name="UnaryType">
  <xs:attributeGroup ref="unaryGroup"/>
  <xs:attribute name="otherAttribute" type="xs:string"/>
</xs:complexType>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜