要求 XML 模式同时存在两个属性或 none
Require either both attributes or none to be present by XML schema
我有一个看起来像这样的元素:
<MyElement start="12.5. 2020" end="6.6 2020" info="Hello world!"/>
还有这个:
<!-- This element still can contain useful info, but is not time-bound -->
<MyElement info="42" />
所以我想设置节点的定义,以便必须存在一个或两个属性。到目前为止,我只是使用文档来记录需求,但如果直接在模式中会更好:
<xsd:complexType name="MyElement">
<xsd:attribute name="start" type="MyDate" use="optional">
<xs:annotation>
<xs:documentation>end date must also be present!</xs:documentation>
</xs:annotation>
</xsd:attribute>
<xsd:attribute name="end" type="MyDate" use="optional">
<xs:annotation>
<xs:documentation>start date must also be present!</xs:documentation>
</xs:annotation>
</xsd:attribute>
<xsd:attribute name="info" type="xsd:string" use="required" />
</xsd:complexType>
XSD1.0
您的约束不能单独在 XSD 1.0 中实施。
XSD 1.1
您的约束可以在 XSD 1.1 中使用 xsd:assert
:
强制执行
<xsd:assert test="(@start and @end) or (not(@start) and not(@end)) "/>
在上下文中显示:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xsd:complexType name="MyElement">
<xsd:attribute name="start" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>end date must also be present!</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="end" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>start date must also be present!</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="info" type="xsd:string" use="required" />
<xsd:assert test="(@start and @end) or (not(@start) and not(@end)) "/>
</xsd:complexType>
<xsd:element name="MyElement" type="MyElement"/>
</xsd:schema>
我有一个看起来像这样的元素:
<MyElement start="12.5. 2020" end="6.6 2020" info="Hello world!"/>
还有这个:
<!-- This element still can contain useful info, but is not time-bound -->
<MyElement info="42" />
所以我想设置节点的定义,以便必须存在一个或两个属性。到目前为止,我只是使用文档来记录需求,但如果直接在模式中会更好:
<xsd:complexType name="MyElement">
<xsd:attribute name="start" type="MyDate" use="optional">
<xs:annotation>
<xs:documentation>end date must also be present!</xs:documentation>
</xs:annotation>
</xsd:attribute>
<xsd:attribute name="end" type="MyDate" use="optional">
<xs:annotation>
<xs:documentation>start date must also be present!</xs:documentation>
</xs:annotation>
</xsd:attribute>
<xsd:attribute name="info" type="xsd:string" use="required" />
</xsd:complexType>
XSD1.0
您的约束不能单独在 XSD 1.0 中实施。
XSD 1.1
您的约束可以在 XSD 1.1 中使用 xsd:assert
:
<xsd:assert test="(@start and @end) or (not(@start) and not(@end)) "/>
在上下文中显示:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xsd:complexType name="MyElement">
<xsd:attribute name="start" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>end date must also be present!</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="end" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>start date must also be present!</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="info" type="xsd:string" use="required" />
<xsd:assert test="(@start and @end) or (not(@start) and not(@end)) "/>
</xsd:complexType>
<xsd:element name="MyElement" type="MyElement"/>
</xsd:schema>