我想用xsd架构文件验证xml文档。 xml文档包含有关Windows服务的信息,我想将Name
属性从Service
设置为唯一值。
这是一个小的xml示例:
<?xml version="1.0" encoding="utf-8"?>
<Services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.de/xml/services">
<Service Name="ALG" StartMode="Manual" State="Stopped">
<DisplayName>xyz</DisplayName>
</Service>
<Service Name="AllUserInstallAgent" StartMode="Manual" State="Stopped">
<DisplayName>xyz</DisplayName>
</Service>
<Service Name="AllUserInstallAgent" StartMode="Manual" State="Stopped">
<DisplayName>xyz</DisplayName>
</Service>
<Service Name="AllUserInstallAgent" StartMode="Manual" State="Stopped">
<DisplayName>xyz</DisplayName>
</Service>
</Services>
我尝试使用xpath:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.de/xml/services" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://example.de/xml/services">
<xsd:element name="Services">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Service">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="DisplayName" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="Name" type="xsd:string" use="required" />
<xsd:attribute name="StartMode" type="xsd:string" use="required" />
<xsd:attribute name="State" type="xsd:string" use="required" />
</xsd:complexType>
<xs:unique name="Unique-Name">
<xs:selector xpath="Service" />
<xs:field xpath="@Name" />
</xs:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xs:schema>
但遗憾的是xml文档仍然有效。请注意,有些记录具有相同的Name
。
我错了什么?我找到了how to make an attribute unique in xml schema?和XML XSD Schema - Enforce Unique Attribute Values in Schema。这里有什么不同?
答案 0 :(得分:2)
它是关于范围和命名空间的。
如果您可视化您的结构,并记住选择器根植于元素中,则约束与...相关联。
您可能会注意到您正在寻找服务中的服务......但这并不存在。因此,第一步是在适当的元素(服务)下移动它。
上述原因仍然不起作用的原因与您使用名称空间的事实有关,并且元素是合格的。这意味着您必须为目标命名空间添加XML名称空间前缀(此处为tns)。所以这是你纠正的XSD:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.de/xml/services" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://example.de/xml/services" xmlns:tns="http://example.de/xml/services">
<xsd:element name="Services">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Service">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="DisplayName" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="Name" type="xsd:string" use="required"/>
<xsd:attribute name="StartMode" type="xsd:string" use="required"/>
<xsd:attribute name="State" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xs:unique name="Unique-Name">
<xs:selector xpath="tns:Service"/>
<xs:field xpath="@Name"/>
</xs:unique>
</xsd:element>
</xs:schema>
将适当地标记您的XML:
Error occurred while loading [], line 11 position 5
There is a duplicate key sequence 'AllUserInstallAgent' for the 'http://example.de/xml/services:Unique-Name' key or unique identity constraint.
Error occurred while loading [], line 14 position 5
There is a duplicate key sequence 'AllUserInstallAgent' for the 'http://example.de/xml/services:Unique-Name' key or unique identity constraint.