我正在尝试编写一个XML Schema,它将验证XML文档,如下所示。出于验证目的,我使用的是xs:unique。我正在尝试将树转换为XML。所以从根本上会有很多分支。在一个路径中从根到叶实体名称必须是唯一的。示例路径:
name0>scenarioDec>Entities>entityMultiAsp>Entity2>entitySpec>Entity2
在此路径中Entity2添加了两次。所以不行。我无法解决这个问题。
另一条道路:
name0>scenarioDec>Events>eventMultiAsp>Aircraft>entitySpec>Entity2
这里的Entity2还可以。因为在此路径中之前没有添加Entiy2。
我尝试过使用
<xs:selector xpath=".//entity"/>
而不是
<xs:selector xpath="*/entity"/>.
如果我使用.//则检查所有路径。但我只需要一条路径唯一性。
我该如何解决这个问题?
<?xml version="1.0" encoding="UTF-8"?>
<entity xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/M:/axm.xsd"
name="name0">
<aspect name="scenarioDec">
<entity name="Environment">
<!-- for test -->
</entity>
<entity name="Entities"> <!-- -->
<multiAspect name="entityMultiAsp">
<entity name="Entity2">
<specialization name="entitySpec">
<entity name="Aircraft"/>
<entity name="Entity2"> <!-- it is not ok because in this path already one Entity2 added before.
name0>scenarioDec>Entities>entityMultiAsp>Entity2>entitySpec>Entity2-->
<var name="var1"></var>
<var name="var2"></var>
</entity>
</specialization>
</entity>
</multiAspect>
</entity>
<entity name="Events">
<multiAspect name="eventMultiAsp">
<entity name="Aircraft"> <!-- it is ok -->
<specialization name="entitySpec">
<entity name="Entity2"> <!-- it is ok because in this path no Entity2 is added before
name0>scenarioDec>Events>eventMultiAsp>Aircraft>entitySpec>Entity2-->
<var name="var1"></var>
<var name="var2"></var>
</entity>
</specialization>
</entity>
</multiAspect>
</entity>
</aspect>
</entity>
上面的XML的XML模式如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:complexType name="aspectType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="entity"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
<xs:complexType name="multiAspectType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="entity"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
<xs:complexType name="specializationType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="entity"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
<xs:complexType name="varType"> <!-- mixed="true" -->
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="entity"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
<xs:element name="entity">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="aspect"/>
<xs:element ref="specialization"/>
<xs:element ref="multiAspect"/>
<xs:element ref="var"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" use="required"/>
<xs:assert test="every $x in .//* satisfies empty($x//*[node-name(.) = node-name($x)])"/>
</xs:complexType>
</xs:element>
<xs:element name="aspect" type="aspectType"/>
<xs:element name="multiAspect" type="multiAspectType"/>
<xs:element name="specialization" type="specializationType"/>
<xs:element name="var" type="varType"/>
</xs:schema>
这里我添加了一张图片,以明确这个概念。
........ 编辑2: ........
<xs:element name="entity">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="aspect"/>
<xs:element ref="specialization"/>
<xs:element ref="multiAspect"/>
<xs:element ref="var"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" use="required"/>
<xs:assert test="empty(../@name)"/>
<!--
Your suggestion:
<xs:assert test="empty(../Entity2)"/>
but for my case may be it will be
<xs:assert test="empty(../@name)"/>
as entity name is not fixed. I have tried both the way but it is not working.
-->
</xs:complexType>
我有四种类型的节点:1。实体,2。MultiAspect,3。Aspect和4.专业化。如果节点是实体,则其后继者是Aspect,MultiAspect或Specialization。如果节点的类型是Aspect,MultiAspect或Specialization,则其子节点的类型为Entity。根节点始终是实体。
我使用上述规则设计了我的XML Schema。我使用“assert”来验证XML文件。我用过
every $x in .//* satisfies empty($x//*[node-name(.) = node-name($x)])
并尝试使用实体而不是*
every $x in .//entity satisfies empty($x//entity[node-name(.) = node- name($x)])
但它一直在显示错误。上面给出了XML Schema。
<?xml version="1.0" encoding="UTF-8"?>
<entity xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/M:/axm.xsd" name="name0">
<aspect name="scenarioDec">
<entity name="Environment">
<multiAspect name="entityMAsp">
<!-- <entity name="EntityTest"></entity> --> <!-- if i add "EntityTest"
as an entity then it shows error but it should be correct -->
</multiAspect>
</entity>
</aspect>
</entity>
当我尝试添加以下行时,在“multiAspect”内部显示错误。
<entity name="EntityTest">
但它应该是正确的。
答案 0 :(得分:1)
感谢您的额外解释,我想我现在明白您要实现的目标。
我不认为这种约束可以在XSD 1.0中表达。在XSD 1.1中,可以使用断言来完成:
<xs:element name="Entity2">
...
<xsl:assert test="empty(.//Entity2)"/>
</xs:element>