xml - Unique attributes with xpath not working -


i validate xml document xsd schema file. xml document contains information windows services, set name attribute service unique value.

here 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 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 xml document still valid. note there records same name.

what did wrong? found how make attribute unique in xml schema? , xml xsd schema - enforce unique attribute values in schema. diffrent here?

it's scope , namespaces.

if visualize structure, , keep in mind selector rooted in element constraint associated with...

enter image description here

you may notice you're looking service under service... not there. so, first step move under appropriate element (services).

enter image description here

the reason why above still doesn't work has fact you're using namespaces, , elements qualified. means have add xml namespace prefix target namespace (tns here). corrected xsd:

<?xml version="1.0" encoding="utf-8" ?> <!-- xml schema generated 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> 

which flag xml appropriately:

error occurred while loading [], line 11 position 5 there duplicate key sequence 'alluserinstallagent' 'http://example.de/xml/services:unique-name' key or unique identity constraint. error occurred while loading [], line 14 position 5 there duplicate key sequence 'alluserinstallagent' 'http://example.de/xml/services:unique-name' key or unique identity constraint. 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -