Xml 只有当另一个元素';s属性是否设置为特定值?
我想问,这是否可能,如果可能,如何实现: 例如,如果我在XML模式定义中有一个complexType元素定义,如下所示:Xml 只有当另一个元素';s属性是否设置为特定值?,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我想问,这是否可能,如果可能,如何实现: 例如,如果我在XML模式定义中有一个complexType元素定义,如下所示: <xsd:element name="tag" type="tag"/> <xs:complexType name="tag"> <xs:choice maxOccurs="unbounded"> <xs:element name="subtag" type="subtag"/> <
<xsd:element name="tag" type="tag"/>
<xs:complexType name="tag">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="subtag"/>
<xs:element name="another_subtag" type="another_subtag"/>
<xs:element name="another_subtag_2" type="another_subtag_2"/>
</xs:choice>
<xs:attribute name="type" type="attr_type"/>
<xs:attribute name="an_attr" type="an_attr"/>
<xs:attribute name="another_attr" type="another_attr"/>
</xs:complexType name="attr_type">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="type_1"/>
<xsd:enumeration value="type_2"/>
<xsd:enumeration value="type_3"/>
</xsd:restriction>
<xsd:simpleType/>
1) 如果标记元素的属性“attr_type”设置为“type_2”,是否可以仅使标记元素的属性“an_attr”成为必需的属性
还有一个问题:
2) 是否可以使complexType“tag”再次包含不同的子元素,例如基于“attr_type”的值?例如:
<tag attr_type="type_1">
只有这个孩子:
<xs:element name="subtag" type="subtag"/>
以及:
<tag attr_type="type_2">
独生子女:
<xs:element name="another_subtag" type="another_subtag"/>
OR
<xs:element name="another_subtag_2" type="another_subtag_2"/>
或
?
如果可能的话,我怎样才能做到这一点
谢谢你的关注
编辑:正如我在这里看到的->
在示例22(在条件类型赋值中)中,可以这样做吗
<!--inline alternative type definitions -->
<element name="TimeTravel" type="TravelType">
<alternative test="@direction='Future'">
<complexType>
<complexContent>
<restriction base="TravelType"
....
<!-- some past travel related elements go here -->
</complexType>
</alternative>
<alternative test="@direction='Past'">
<complexType>
<complexContent>
<restriction base="TravelType"
....
<!-- some future travel related elements go here -->
</complexType>
</alternative>
</element>
OR
<!--Named alternative type definitions -->
<element name="TimeTravel" type="TravelType">
<alternative test="@direction='Future' type="FutureTravelType"/>
<alternative test="@direction='Past' type="PastTravelType"/>
</element>
在XSD1.1中,可以使用“条件类型赋值”的新特性。这允许您将元素的类型定义为其属性值的函数
XSD 1.1在Altova、Saxon和Xerces中实现。在XSD 1.1中可以使用“条件类型分配”的新功能。这允许您将元素的类型定义为其属性值的函数
XSD 1.1在Altova、Saxon和Xerces中实现。否,因为条件类型分配(在另一个答案中提到)只将类型分配给元素。因此命名为条件类型赋值。此外,当使用条件类型赋值时,元素的类型由它自己的属性决定,而不是由其他元素的属性决定
即使使用带条件类型赋值的XPATH表达式,也只能访问正在验证的元素的属性。它不能访问其父代或祖先,甚至不能像断言那样访问其子代或后代
[编辑]发布的主要问题是,根据另一个元素的属性,是否需要第1个属性,答案是否
这需要比XMLSchema1.1更高级别的验证。也许是Schematron???我不知道。有时候答案不是你想听的
参考:Priscilla Walmsley的权威XML模式,第二版,第378-379页
这里总结了无数次关于XML验证的类似问题。模式1.1中的新添加在范围上非常有限 否,因为条件类型分配(在另一个答案中提到)只将类型分配给元素。因此命名为条件类型赋值。此外,当使用条件类型赋值时,元素的类型由它自己的属性决定,而不是由其他元素的属性决定
即使使用带条件类型赋值的XPATH表达式,也只能访问正在验证的元素的属性。它不能访问其父代或祖先,甚至不能像断言那样访问其子代或后代
[编辑]发布的主要问题是,根据另一个元素的属性,是否需要第1个属性,答案是否
这需要比XMLSchema1.1更高级别的验证。也许是Schematron???我不知道。有时候答案不是你想听的
参考:Priscilla Walmsley的权威XML模式,第二版,第378-379页
这里总结了无数次关于XML验证的类似问题。模式1.1中的新添加在范围上非常有限 让我们再试一次,因为我的答案有争议,所以最好再详细说明一次。编辑我的原始答案会使随后的评论难以理解,只会增加混乱
问题1:仅当标记元素的属性“attr_type”设置为“type_2”时,是否可以使标记元素的属性“an_attr”成为必需的
回答1:是的。您可以这样做(已测试;从原始类型修改以避免引用不相关和未定义的类型):
问题2:
是否可以使complexType“tag”再次包含不同的子元素,例如基于“attr_type”的值
答复2:
是,例如(类似测试):
问题0:仅当另一个元素(F)的属性设置为特定值时,是否可以将XML元素(E)的属性定义为必需的
回答0:这取决于两个元素E和F的关系。如果F是E的祖先,那么可以在F的声明中使用条件类型赋值。如果E和F是兄弟或更遥远的关系,那么不能使用条件类型赋值,但这可以通过附加到E和F共同祖先的断言来实现。让我们再试一次,因为我的答案有争议,最好再详细说明一次。编辑我的原始答案会使随后的评论难以理解,只会增加混乱
问题1:是否有可能
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag" type="tagType">
<xs:alternative test="@attr_type='type_2'" type="tagTypeWithMandatoryAttr"/>
</xs:element>
<xs:complexType name="tagType">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="xs:anyType"/>
<xs:element name="another_subtag" type="xs:anyType"/>
<xs:element name="another_subtag_2" type="xs:anyType"/>
</xs:choice>
<xs:attribute name="type" type="xs:string"/>
<xs:attribute name="an_attr" type="xs:string"/>
<xs:attribute name="another_attr" type="xs:string"/>
</xs:complexType>
<xs:complexType name="tagTypeWithMandatoryAttr">
<xs:complexContent>
<xs:restriction base="tagType">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="xs:anyType"/>
<xs:element name="another_subtag" type="xs:anyType"/>
<xs:element name="another_subtag_2" type="xs:anyType"/>
</xs:choice>
<xs:attribute name="type" type="xs:string"/>
<xs:attribute name="an_attr" type="xs:string" use="required"/>
<xs:attribute name="another_attr" type="xs:string"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag" type="xs:anyType">
<xs:alternative test="@attr_type='type_2'" type="tagType1"/>
<xs:alternative type="tagType2"/>
</xs:element>
<xs:complexType name="tagType1">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="xs:anyType"/>
<xs:element name="another_subtag" type="xs:anyType"/>
<xs:element name="another_subtag_2" type="xs:anyType"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="tagType2">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="xs:anyType"/>
<xs:element name="another_subtag" type="xs:anyType"/>
<xs:element name="another_subtag_2" type="xs:anyType"/>
</xs:choice>
</xs:complexType>
</xs:schema>