I’ll admit, I’m an XML newbie. I’m having trouble validating some xml against a schema. Here is the relevant part of my schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.me.com/orpm/kpi/automation/report"
targetNamespace="http://www.me.com/orpm/kpi/automation/report">
<xs:attribute name="Pattern">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Exact"/>
<xs:enumeration value="Replace"/>
<xs:enumeration value="Regex"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="NameValue">
<xs:all>
<xs:element name="Value" type="xs:string"/>
</xs:all>
<xs:attribute ref="Pattern"/>
</xs:complexType>
<xs:element name="KpiReport">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="NameValue"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here is the failed xml:
<?xml version="1.0"?>
<KpiReport xmlns="http://www.me.com/orpm/kpi/automation/report"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.me.com/orpm/kpi/automation/report filename.xsd">
<Name Pattern="Exact">
<Value>Test</Value>
</Name>
</KpiReport>
It fails with this error:
Description: cvc-complex-type.2.4.a: Invalid content was found starting with element 'Name'. One of '{Name}' is expected.
I am lost on this. Please help.
There are a couple of things going on, and a couple of possible solutions to your issue.
The solution that you can use will depend on whether you can change the XML, the XSD or both.
Below is some XML that passes your schema validation:
The key difference is that in your xsd you have not defined the Name element within the http://www.me.com/orpm/kpi/automation/report namespace. I’m not entirely sure what is happening but think that having the Pattern attribute at the schema level has confused things. By assing the namespace prefix ns0: are telling the validator that KpiReport and Pattern are both in that namespace, but Name is not.
This solution may not fit, so the other option is to tweak your schema to accept your example XML. Below is a schema that works:
Things to note are that the Pattern attribute is now moved into the NameValue complexType, and that Name and Value are now specified as form=”qualified”. Qualified means that these elements require namespace prefixes (in this case, the prefix is the default, or none).
I’m not 100% sure why this is required – my XSD knowledge is good enough to get to this point, but not know quite what is going on. I usually use tools to generate my schemas and then tweak until I’ve got things right.
Hopefully this helps you, or that someone with sharper XSD chops can fill in the gaps.