2013-05-14 4 views
0

xsd 스키마 파일로 XML 문서의 유효성을 검사하고 싶습니다. xml 문서에는 Windows 서비스에 대한 정보가 포함되어 있으므로 Name 속성을 Service에서 고유 한 값으로 설정하고 싶습니다. 내가 XPath는 함께 다음과 같은 노력xpath가 작동하지 않는 고유 속성

<?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> 

:

<?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 문서가 여전히 유효 여기

작은 XML 예제입니다. Name 같은 레코드가 있습니다.

무엇이 잘못 되었나요? 나는이 how to make an attribute unique in xml schema?XML XSD Schema - Enforce Unique Attribute Values in Schema을 발견했다. 여기 다른 점은 무엇입니까?

답변

1

범위 및 네임 스페이스에 대한 것입니다.

당신이 당신의 구조를 시각화하고, 선택이 제약 조건과 관련된 요소에 뿌리를두고 있다는 사실을 양지 경우

...

enter image description here

당신이 서비스를 찾고있는 것을 알 수 있습니다 서비스 아래에 있지 않습니다. 그래서, 첫 번째 단계는 적절한 요소 (서비스) 아래로 이동하는 것입니다.

enter image description here

위는 여전히 작동하지 않는 이유는 네임 스페이스를 사용하고, 요소 자격이 있다는 사실과 관련이있다. 즉, 대상 네임 스페이스에 대한 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. 
+0

감사합니다, 나는 즉시 내일을 테스트합니다! 어떤 도구를 사용 했습니까? xmlns, xmlns : tns 및 targetNamespace가 동일한 값을 가진 것을 보았습니다. 필요한 것입니까? 나 또한 네임 스페이스없이 시도했지만 targetNamespace를 제거했지만 작동하지 않는 이유는 무엇입니까? 생존의 날을 보내십시오. – hofmeister

+0

@hofmeister, targetNamespace는 요구 사항과 일치해야합니다. 같은 이름으로 기본 네임 스페이스를 정의 할 필요가 없습니다. 그러나 많은 사람들이 모범 사례라고 믿습니다. tns는 임의적이지만 여전히 targetNamespace에 대한 별칭을 정의하고 요소를 정규화해야하므로 접두어가없는 경우 selector/field의 XPath는 기본 네임 스페이스가 아닌 네임 스페이스를 사용합니다. 이 도구는 우리가 개발하는 [QTAssistant] (http://www.paschidev.com)입니다. –

+0

하지만 서비스 아래에서 제약 조건을 이동하고 기본 네임 스페이스 xmlns를 설정하면 왜 위장이 작동하지 않습니까? 왜 xmls, xmls : prefix와 targetnamespace가 필요한지 이해할 수 없습니다. 설명해 주시면 좋을 것입니다! – hofmeister

관련 문제