我尝试定义一个可以在后续元素定义中引用的属性。
<xs:attribute name="ridref" type="xs:string"/>
后来我像这样使用它:
<xs:element name="coordRegRef">
<xs:complexType>
<!--xs:attribute name="ridref" type="xs:string"/ this works but I want to use ref -->
<xs:attribute ref="ridref" use="required"/>
</xs:complexType>
</xs:element>
XSD使用xmllint编译好
xmllint --schema pc-ar.xsd pc-ar.xml
但是xmllint说
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef', attribute 'ridref': The attribute 'ridref' is not allowed.
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef': The attribute '{http://pc-ar.xsd}ridref' is required but missing.
我认为这意味着我的XML文件必须使用ridref
的命名空间<coordRegRef fix:ridref="111"/>
(这确实有效,但不受欢迎)而不是
<coordRegRef ridref="111"/>
为什么?
我的XSD
<?xml version="1.0"?>
<!-- validate: xmllint - -schema pc-ar.xsd pc-ar.xml -->
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pc-ar.xsd"
elementFormDefault="qualified"
xmlns="http://pc-ar.xsd"
xmlns:fix="http://pc-ar.xsd">
<!--xs:attribute name="rid" type="xs:integer"/-->
<!--xs:attributeGroup name="ridGroup">
<xs:attribute name="rid" type="xs:integer">
<xs:annotation>
<xs:documentation>entry id</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup-->
<xs:element name="name" type="xs:string"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="idref" type="xs:IDREF"/>
<xs:attribute name="ridref" type="xs:string"/>
<xs:element name="coordRegRef">
<xs:complexType>
<!--xs:attribute name="ridref" type="xs:string"/works-->
<xs:attribute ref="ridref" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="assetRegRef">
<xs:complexType>
<xs:sequence>
<xs:element name="ridref2" type="xs:string"/>
</xs:sequence>
<!--xs:attribute name="ridref" type="xs:string"/works-->
<xs:attribute name="ridref" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="lat">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:minInclusive value="-40"/>
<xs:maxInclusive value="-30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="lon" type="xs:decimal"/>
<xs:complexType name="coordRegTableType">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="lat"/>
<xs:element ref="lon"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:string"/>
<!--xs:attribute ref="fix:id" use="required"/-->
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="assetRegTableType">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="coordRegRef"/>
<xs:element ref="assetRegRef"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:string"/>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="coordReg" type="fix:coordRegTableType">
<xs:unique name="coordRegNameUniq">
<xs:selector xpath="*"/>
<xs:field xpath="fix:name"/>
</xs:unique>
</xs:element>
<xs:element name="assetReg" type="assetRegTableType">
<xs:unique name="assetRegNameUniq">
<xs:selector xpath="fix:entry"/>
<xs:field xpath="fix:name"/>
</xs:unique>
</xs:element>
<xs:element name="assetExchange">
<xs:complexType>
<xs:sequence>
<xs:element ref="coordReg"/>
<xs:element ref="assetReg"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:integer"/>
<!--xs:attribute ref="id" use="required"/-->
</xs:complexType>
<xs:key name="coordRegKey">
<xs:selector xpath="fix:coordReg/fix:entry"/>
<xs:field xpath="@rid"/>
</xs:key>
<xs:key name="assetRegKey">
<xs:selector xpath="fix:assetReg/fix:entry"/>
<xs:field xpath="@rid"/>
</xs:key>
<xs:keyref name="assetRegKeyRef" refer="assetRegKey">
<xs:selector xpath=".//fix:assetRegRef"/>
<xs:field xpath="@ridref"/>
</xs:keyref>
<xs:keyref name="assetRegKeyRef2" refer="assetRegKey">
<!--xs:selector xpath=".//fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/*/fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/fix:entry/fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/*/fix:assetRegRef"/works-->
<xs:selector xpath=".//fix:assetRegRef"/> <!-- any assetRefRef -->
<xs:field xpath="fix:ridref2"/>
</xs:keyref>
<xs:keyref name="coordRegKeyRef" refer="coordRegKey">
<xs:selector xpath=".//fix:coordRegRef"/>
<xs:field xpath="@ridref"/>
</xs:keyref>
</xs:element>
</xs:schema>
我的XML测试文件
<?xml version="1.0"?>
<assetExchange
xmlns="http://pc-ar.xsd"
xmlns:fix="http://pc-ar.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pc-ar.xsd pc-ar.xsd"
rid="1">
<coordReg>
<entry id="1" rid="111">
<name>sitea</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
<entry id="2" rid="222">
<name>siteb</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
<entry id="3" rid="333">
<name>sitec</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
</coordReg>
<assetReg>
<entry id="11" rid="11">
<name>deva</name>
<coordRegRef fix:ridref="111"/>
<assetRegRef ridref="aa"><ridref2>aa</ridref2></assetRegRef>
</entry>
<entry id="22" rid="22">
<name>devb</name>
<coordRegRef ridref="2212"/>
<assetRegRef ridref="11"><ridref2>11</ridref2></assetRegRef>
</entry>
<entry id="33" rid="33">
<name>devc</name>
<coordRegRef ridref="333"/>
<assetRegRef ridref="44"><ridref2>44</ridref2></assetRegRef>
</entry>
</assetReg>
</assetExchange>
答案 0 :(得分:5)
正如您所注意到的那样,从验证错误中可以看出,解析器确实需要属性ridref
,但它必须位于命名空间中。
原因是在XML Schemas中必须限定所有全局元素,属性或类型定义。这样,即使您已设置attributeFormDefault="unqualified"
,所有全局定义的属性也将以名称空间为前缀。解决方法是在<xs:attributeGroup>
(或全局类型)中单独定义此类属性。在这种情况下,属性组的名称将获取附加到其的名称空间,而不是其中的属性名称。然后,您可以使用ref
来引用这些属性组,并获取全局可用的属性,而无需将它们放在命名空间中。
答案 1 :(得分:4)
由于您要声明全局属性,因此它们是{http://pc-ar.xsd}
命名空间的一部分。 XML中没有前缀的属性总是属于无命名空间。因此,如果您不想在本地声明属性,并且也不希望在XML中为属性添加前缀,则可以选择将它们包含在属性组中:
<xs:attributeGroup name="required-ridref">
<xs:attribute name="ridref" type="xs:string" use="required"/>
</xs:attributeGroup>
<xs:attributeGroup name="not-required-ridref">
<xs:attribute name="ridref" type="xs:string" use="required"/>
</xs:attributeGroup>
并引用您将引用该属性的组:
<xs:element name="coordRegRef">
<xs:complexType>
<xs:attributeGroup ref="required-ridref" />
</xs:complexType>
</xs:element>
<xs:element name="assetRegRef">
<xs:complexType>
<xs:sequence>
<xs:element name="ridref2" type="xs:string"/>
</xs:sequence>
<xs:attributeGroup ref="not-required-ridref"/>
</xs:complexType>
</xs:element>