我正在尝试验证一个大致如下所示的一堆 XML 文件(示例完整文件位于 Gist):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- ... -->
<ProjectExtensions>
<Borland.Personality>Default.Personality</Borland.Personality>
<Borland.ProjectType />
<BorlandProject>
<BorlandProject xmlns=""> <Default.Personality> </Default.Personality> </BorlandProject></BorlandProject>
</ProjectExtensions>
<!-- ... -->
</Project>
我知道嵌套<BorlandProject xmlns="">
很奇怪,但是我们存储库中的很多 XML 文件都是这样的,我想验证它们是否正常。
这些文件使用 MSBuild 定义的子集。到目前为止,我已经尝试过这样的事情(再次:Gist 上的完整文件):
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0"
targetNamespace="http://schemas.microsoft.com/developer/msbuild/2003"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Project" type="ProjectType"/>
<!-- ... -->
<xsd:complexType name="ProjectExtensionsType">
<xsd:sequence>
<xsd:element name="Borland.Personality" type="xsd:string"/>
<xsd:element name="Borland.ProjectType" type="xsd:string"/>
<xsd:element name="BorlandProject" type="BorlandProjectType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BorlandProjectType">
<xsd:choice>
<xsd:sequence>
<xsd:element name="Default.Personality" type="xsd:string"/>
</xsd:sequence>
<xsd:element name="BorlandProject" type="BorlandProjectType"/>
</xsd:choice>
</xsd:complexType>
<!-- ... -->
</xsd:schema>
使用如下错误消息验证 barfs:
Error: The element 'BorlandProject' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'
has invalid child element 'BorlandProject'.
List of possible elements expected:
'Default.Personality, BorlandProject' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'.
<BorlandProject>
我想这是因为奇怪的xmlns=""
内心<BorlandProject xmlns="">
。
两个问题:
- 我的
xmlns=""
假设对吗? - 如何验证此
<BorlandProject xmlns="">
元素?
编辑
感谢ColdFusion的回答。这就是我最终所做的,因为BorlandProject
既可以是合格的,也可以是不合格的:
<xsd:complexType name="BorlandProjectType">
<xsd:choice>
<xsd:sequence>
<xsd:element name="Default.Personality" type="xsd:string"/>
</xsd:sequence>
<!-- Delphi <= 2010 can have a nested `BorlandProject` element, sometimes unqualified (with xmlns=""), sometimes qualified -->
<xsd:choice>
<xsd:element name="BorlandProject" form="unqualified" type="BorlandProjectTypeUnqualified"/>
<xsd:element name="BorlandProject" type="BorlandProjectTypeQualified"/>
</xsd:choice>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="BorlandProjectTypeUnqualified">
<xsd:sequence>
<xsd:element name="Default.Personality" form="unqualified" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BorlandProjectTypeQualified">
<xsd:sequence>
<xsd:element name="Default.Personality" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>