xml: 使用条件属性
xml: using conditioned attributes
我正在寻找一种实现方案的方法,其中元素只有在其枚举具有特定值时才具有属性“主题”,如即将到来的示例所示:
<article>
<type>Blogpost</type>
</article>
<article>
<type topic='news'>Article</type>
</article>
<article>
<type>Comment</type>
</article>
挑战在于,只有在选择值“文章”或“纪录片”时才使用该属性。此外,我只希望预定义列表中的枚举有效。如果选择了正确的枚举,我希望在所有其他情况下都需要该属性,我根本不想拥有该属性。
到目前为止,我尝试了以下方案,但它根本不起作用:
<xs:element name="type" minOccurs="0">
<xs:alternative test="@name='type'" type="type1"/>
<xs:alternative test="@name='type'" type="type2"/>
<xs:alternative type="xs:error"/>
</xs:element>
(...)
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType-->
我总是收到错误消息,指出任何所选元素都是“不是“错误”的有效值”。我尝试使用该属性的另一个示例但是它允许任何文本并且不关心枚举限制使其基本上无用:
<xs:element name="type" minOccurs="0">
<xs:alternative test="@ItemType='type'" type="type1"/>
<xs:alternative test="@ItemType='type'" type="type2"/>
</xs:element>
(...)
当同一个元素实际上是正确的枚举并且对枚举的限制仍然有效时,我该如何做到这一点?我使用 xs:alternative 的方法是否正确,以便一次使用相同的元素,一次不使用属性?
为了使用 xs:assert.
,我无法找出正确的测试条件
我现在有了一个有用的答案 - 对于没有用的评论,我们深表歉意!
这个问题几乎与你的相同:
.在此基础上,我建议您尝试这样的操作(未测试,因为我没有 XSD1.1 处理器)
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'/>
<xs:assert test="not ( @topic or ( @topic and ($value eq 'Article' or $value eq 'Documentary'))"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>```
再次感谢您的方法@kimbert。
在您的帮助下,我能够找出满足需求的断言。我只需要稍微调整一下你的想法:
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics"/>
<xs:assert test="@topic and ($value eq 'Article' or $value eq 'Documentary') or not ($value eq 'Article' or $value eq 'Documentary')"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
像这样,编译器现在总是需要属性“topic”,以防要求值“Article”或“Documentary”。在所有其他情况下,该属性将被忽略。
我正在寻找一种实现方案的方法,其中元素只有在其枚举具有特定值时才具有属性“主题”,如即将到来的示例所示:
<article>
<type>Blogpost</type>
</article>
<article>
<type topic='news'>Article</type>
</article>
<article>
<type>Comment</type>
</article>
挑战在于,只有在选择值“文章”或“纪录片”时才使用该属性。此外,我只希望预定义列表中的枚举有效。如果选择了正确的枚举,我希望在所有其他情况下都需要该属性,我根本不想拥有该属性。 到目前为止,我尝试了以下方案,但它根本不起作用:
<xs:element name="type" minOccurs="0">
<xs:alternative test="@name='type'" type="type1"/>
<xs:alternative test="@name='type'" type="type2"/>
<xs:alternative type="xs:error"/>
</xs:element>
(...)
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType-->
我总是收到错误消息,指出任何所选元素都是“不是“错误”的有效值”。我尝试使用该属性的另一个示例但是它允许任何文本并且不关心枚举限制使其基本上无用:
<xs:element name="type" minOccurs="0">
<xs:alternative test="@ItemType='type'" type="type1"/>
<xs:alternative test="@ItemType='type'" type="type2"/>
</xs:element>
(...)
当同一个元素实际上是正确的枚举并且对枚举的限制仍然有效时,我该如何做到这一点?我使用 xs:alternative 的方法是否正确,以便一次使用相同的元素,一次不使用属性? 为了使用 xs:assert.
,我无法找出正确的测试条件我现在有了一个有用的答案 - 对于没有用的评论,我们深表歉意!
这个问题几乎与你的相同:
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics" use='required'/>
<xs:assert test="not ( @topic or ( @topic and ($value eq 'Article' or $value eq 'Documentary'))"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>```
再次感谢您的方法@kimbert。
在您的帮助下,我能够找出满足需求的断言。我只需要稍微调整一下你的想法:
<xs:element name="type" type="type2" minOccurs="0"/>
<xs:complexType name="type2">
<xs:simpleContent>
<xs:extension base="type1">
<xs:attribute name="topic" type="topics"/>
<xs:assert test="@topic and ($value eq 'Article' or $value eq 'Documentary') or not ($value eq 'Article' or $value eq 'Documentary')"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="type1">
<xs:restriction base="xs:string">
<xs:enumeration value="Article"/>
<xs:enumeration value="Documentary"/>
<xs:enumeration value="Blogpost"/>
<xs:enumeration value="Comment"/>
<xs:enumeration value="Sentence"/>
</xs:restriction>
</xs:simpleType>
像这样,编译器现在总是需要属性“topic”,以防要求值“Article”或“Documentary”。在所有其他情况下,该属性将被忽略。