问题的原因: 需要在schema里面加入一句xmlns="http://my.com"
这样一来在targetNamespace里面定义的各种自定义的标签,就能被当前的xsd自身引用了。
修改后的xsd如下:<?xml version="1.0" encoding="utf-8"?>
<x:schema targetNamespace="http://my.com"
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:y="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns="http://my.com">
<x:element name="a">
<x:complexType>
<x:sequence>
<x:element name="orderItem">
<x:complexType>
<x:sequence/>
<x:attribute name="MyId" type="x:string" use="required"/>
</x:complexType>
</x:element>
</x:sequence>
</x:complexType>
</x:element>
</x:schema>xml文件如下:<?xml version="1.0"?>
<x:a xmlns:x="http://my.com" xmlns:y="http://my.com"
xmlns:my="http://www.w3.org/2001/XMLSchema-instance" my:noNamespaceSchemaLocation="d:\my.xsd">
<x:orderItem MyId="abc"/>
</x:a>