问题
I am getting this error when I am validating XML against my XSD. Both schema and instance are valid and I am able to validate them in XML parsers but I am getting this error in Java:
cvc-elt.1: Cannot find the declaration of element 'fieldsMapper'
Below is my schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="https://www.company.com/mine" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fieldsMapper" type="mine:fieldsMapperType" xmlns:mine="https://www.company.com/mine"/>
<xs:complexType name="sourceFieldType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:attribute type="xs:string" name="type" use="optional"/>
<xs:attribute type="xs:string" name="inputFormat" use="optional"/>
<xs:attribute type="xs:string" name="default" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="fieldType">
<xs:sequence>
<xs:element type="mine:sourceFieldType" name="sourceField" minOccurs="0" xmlns:mine="https://www.company.com/mine"/>
<xs:element type="xs:string" name="value" minOccurs="0"/>
<xs:element type="xs:string" name="groovy" minOccurs="0"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:attribute type="xs:string" name="type" use="optional"/>
<xs:attribute type="xs:boolean" name="sasDate" use="optional"/>
<xs:attribute type="xs:string" name="outputFormat" use="optional"/>
</xs:complexType>
<xs:complexType name="fieldsType">
<xs:sequence>
<xs:element type="mine:fieldType" name="field" maxOccurs="unbounded" minOccurs="0" xmlns:mine="https://www.company.com/mine">
<xs:annotation>
<xs:documentation>Nested/Mapped field in a Java bean or Map as target Nested/Mapped field in a Java bean or Map as source Indexed field as target (in an array or List) Indexed field as source (in an array or List)</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fieldsMapperType">
<xs:sequence>
<xs:element type="mine:fieldsType" name="fields" xmlns:mine="https://www.company.com/mine"/>
</xs:sequence>
<xs:attribute type="xs:string" name="targetType"/>
<xs:attribute type="xs:string" name="sourceType"/>
<xs:attribute type="xs:string" name="id"/>
</xs:complexType>
</xs:schema>
My instance document is below:
<?xml version="1.0" encoding="UTF-8"?>
<fieldsMapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.company.com/mine/fieldsMapper fieldsMapper.xsd"
xmlns="https://www.company.com/mine"
sourceType="java.util.Map"
targetType="java.util.Map"
id="identityValidationInputMapper">
<fields>
<field name="msg_date1" type="java.util.Date">
<sourceField name="msg_date" type="java.lang.String" inputFormat="yyyyMMdd" default="20200407"/>
</field>
<field name="msg_date2" type="java.util.Date">
<sourceField name="msg[msg_date_field]" type="java.lang.String" inputFormat="yyyyMMdd"/>
</field>
<field name="msg_date3" type="java.util.Date">
<sourceField name="input_array[0]" type="java.lang.String" inputFormat="yyyyMMdd"/>
</field>
<field name="msg_constant_text" type="java.lang.String">
<value>BLAH</value>
</field>
<field name="msg_text" type="java.lang.String">
<value>
<![CDATA[
Just a long text value
]]>
</value>
</field>
<field name="order_amount">
<groovy>
double taxPercent = sourceFields['tax_percent'] == null ? 5 : Double.valueOf(sourceFields['tax_percent'])
double txnAmount = sourceFields['txn_amount'] == null ? 0 : Double.valueOf(sourceFields['txn_amount'])
return taxPercent * txnAmount
</groovy>
</field>
<field name="msg_index" type="java.lang.Integer">
<sourceField name="input_index" type="java.lang.String" default="10"/>
</field>
</fields>
</fieldsMapper>
I tried so many variations of schema location and xmlns but I am getting that error no matter what I tried.
回答1:
The target namespace of your XSD is https://www.company.com/mine
.
You have this as the default namespace of the root element (fieldsMapper
) of your XML.
So far so good.
But your schemaLocation uses a different namespace URI:
xsi:schemaLocation="https://www.company.com/mine/fieldsMapper fieldsMapper.xsd"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Change that to
xsi:schemaLocation="https://www.company.com/mine fieldsMapper.xsd"
and your problem will be solved.
See also
- How to reference a local XML Schema file correctly?
- How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
来源:https://stackoverflow.com/questions/62345530/xsd-schemalocation-targetnamespace-default-xml-namespace-matching