XML Schema 属性引用

3

我有这个XML模式

 <?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>

然后是使用它的XML文件:

<?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>

如果我更改XML模式以使用ref =“”属性,如下所示:

...
<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>
 </xs:simpleType>

然后我的xml变成了:
<inv_constraint xmlns:ns1="http://hidden/abc" ns1:unaryOperator="not">

但我想使用ref,并让我的XML看起来像这样:
    <inv_constraint unaryOperator="not">

我该怎么做呢? 谢谢

1个回答

2
在XML模式中,所有全局元素、属性或类型定义都必须是限定的。因此,即使您将attributeFormDefault定义为“不限定”,所有全局定义的属性也将带有命名空间前缀。解决方法是在属性组或全局类型中定义此属性,然后您可以引用此命名组或扩展此命名类型。
<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>

当您需要使用此属性时,请参考attributeGroup而不是attribute。Attribute group不需要包含元素使用的所有属性,以下内容也应该有效:
<xs:complexType name="UnaryType">
  <xs:attributeGroup ref="unaryGroup"/>
  <xs:attribute name="otherAttribute" type="xs:string"/>
</xs:complexType>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接