XSD schemaLocation,targetNamespace,默认XML名称空间匹配

时间:2020-06-12 13:44:40

标签: java spring-boot xsd xsd-validation xml-validation

针对XSD验证XML时,出现此错误。模式和实例均有效,我能够在XML解析器中对其进行验证,但在Java中却遇到此错误:

cvc-elt.1:找不到元素'fieldsMapper'的声明

以下是我的架构:

<?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>

我的实例文档如下:

<?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>

我尝试了许多模式位置和xmlns的变体,但是无论尝试如何,都会遇到该错误。

1 个答案:

答案 0 :(得分:1)

您XSD的目标名称空间是https://www.company.com/mine

您将其作为XML根元素(fieldsMapper)的默认命名空间。

到目前为止一切顺利。

但是您的schemaLocation使用不同的名称空间URI:

xsi:schemaLocation="https://www.company.com/mine/fieldsMapper fieldsMapper.xsd"
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

将其更改为

xsi:schemaLocation="https://www.company.com/mine fieldsMapper.xsd"

您的问题将得到解决。

另请参见

相关问题