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을 발견했다. 여기 다른 점은 무엇입니까?
감사합니다, 나는 즉시 내일을 테스트합니다! 어떤 도구를 사용 했습니까? xmlns, xmlns : tns 및 targetNamespace가 동일한 값을 가진 것을 보았습니다. 필요한 것입니까? 나 또한 네임 스페이스없이 시도했지만 targetNamespace를 제거했지만 작동하지 않는 이유는 무엇입니까? 생존의 날을 보내십시오. – hofmeister
@hofmeister, targetNamespace는 요구 사항과 일치해야합니다. 같은 이름으로 기본 네임 스페이스를 정의 할 필요가 없습니다. 그러나 많은 사람들이 모범 사례라고 믿습니다. tns는 임의적이지만 여전히 targetNamespace에 대한 별칭을 정의하고 요소를 정규화해야하므로 접두어가없는 경우 selector/field의 XPath는 기본 네임 스페이스가 아닌 네임 스페이스를 사용합니다. 이 도구는 우리가 개발하는 [QTAssistant] (http://www.paschidev.com)입니다. –
하지만 서비스 아래에서 제약 조건을 이동하고 기본 네임 스페이스 xmlns를 설정하면 왜 위장이 작동하지 않습니까? 왜 xmls, xmls : prefix와 targetnamespace가 필요한지 이해할 수 없습니다. 설명해 주시면 좋을 것입니다! – hofmeister