I would like to validate a xml document with an xsd schema file. The xml document contains information about windows services, I would like to set the Name
attribute from Service
to a unique value.
Here is a small xml example:
<?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>
I tried following with 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>
But sadly the xml document is still valid. Note that there are some records with the same Name
.
What did I wrong? I found this how to make an attribute unique in xml schema? and XML XSD Schema - Enforce Unique Attribute Values in Schema. What is diffrent here?