Xml 如何使用模式基于属性值验证元素?
我尝试验证的XML如下所示:Xml 如何使用模式基于属性值验证元素?,xml,validation,schema,xsd,Xml,Validation,Schema,Xsd,我尝试验证的XML如下所示: <root> <element attribute="foo"> <bar/> </element> <element attribute="hello"> <world/> </element> </root> 如何使用模式验证这一点 注: 元素只能在attribute=“foo”时包含条 元素只能包
<root>
<element attribute="foo">
<bar/>
</element>
<element attribute="hello">
<world/>
</element>
</root>
如何使用模式验证这一点
注:
元素只能在attribute=“foo”时包含条
元素只能包含世界当attribute=“hello”在XML Schema 1.0中不能这样做时。在XMLSchema1.1中,您将能够使用来实现这一点,但我猜您希望现在就可以使用 您可以将其用作第二层验证,这将允许您测试关于XML文档的任意XPath断言。有一篇相当古老的文章,你可能会觉得它很有用 你可以这样做:
<rule context="element">
<report test="@attribute = 'foo' and *[not(self::bar)]">
This element's attribute is 'foo' but it holds an element that isn't a bar.
</report>
<report test="@attribute = 'hello' and *[not(self::world)]">
This element's attribute is 'hello' but it holds an element that isn't a world.
</report>
</rule>
此元素的属性为“foo”,但它包含的元素不是条。
此元素的属性为“hello”,但它包含的元素不是世界。
当然,您也可以切换到,它在睡眠中执行以下操作:
<element name="element">
<choice>
<group>
<attribute name="attribute"><value>foo</value></attribute>
<element name="bar"><empty /></element>
</group>
<group>
<attribute name="attribute"><value>hello</value></attribute>
<element name="world"><empty /></element>
</group>
</choice>
</element>
福
你好