XSD schemaLocation、targetNamespace、默认 XML 命名空间匹配 [英] XSD schemaLocation, targetNamespace, default XML namespace matching

查看:44
本文介绍了XSD schemaLocation、targetNamespace、默认 XML 命名空间匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我根据 XSD 验证 XML 时出现此错误.模式和实例都是有效的,我可以在 XML 解析器中验证它们,但在 Java 中出现此错误:

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: 找不到元素fieldsMapper"的声明

cvc-elt.1: Cannot find the declaration of element '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 的变体,但无论我尝试什么,我都会遇到该错误.

I tried so many variations of schema location and xmlns but I am getting that error no matter what I tried.

推荐答案

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

The target namespace of your XSD is https://www.company.com/mine.

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

You have this as the default namespace of the root element (fieldsMapper) of your XML.

到目前为止一切顺利.

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

But your schemaLocation uses a different namespace URI:

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

改成

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

你的问题就会得到解决.

and your problem will be solved.

这篇关于XSD schemaLocation、targetNamespace、默认 XML 命名空间匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆