XML Schema校验报错:元素不允许出现id属性,求解决办法
Hey there! Let's get this validation error sorted out for you. The error you're seeing (cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'System') has two key issues we need to address:
1. The id attribute isn't declared in your XSD
Your XML's <A:System> element includes an id attribute, but your current XSD doesn't specify that this attribute is allowed for the System element. To fix this, you need to add an <xs:attribute> definition inside the xs:complexType block of the System element.
Updated XSD with id attribute support:
<xs:element name="System" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="SystemCd" /> </xs:sequence> <!-- Add this to declare the 'id' attribute --> <xs:attribute name="id" type="xs:string" use="required" /> </xs:complexType> </xs:element>
- Use
use="required"if theidattribute must always be present (like in your XML example). If it's optional, change this touse="optional"or omit theuseattribute entirely (optional is the default behavior).
2. Mismatched opening/closing XML tags
Looking at your XML snippet, there's a critical tag mismatch: you start with <A:System> but close with </A:SourceSystem>. This will cause a separate validation error even after fixing the attribute issue. Correct the closing tag to match the opening one:
Corrected XML:
<A:System id="systemName"> <A:SystemCd>001</A:SystemCd> </A:System>
Apply both these fixes, and your XML should validate against the updated XSD without any errors.
内容的提问来源于stack exchange,提问作者Robert Price

