【问题标题】:XSD schemaLocation, targetNamespace, default XML namespace matchingXSD schemaLocation、targetNamespace、默认 XML 命名空间匹配
【发布时间】:2020-10-02 08:16:51
【问题描述】:

我在针对我的 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 的变体,但无论我尝试什么,我都会收到该错误。

【问题讨论】:

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


    【解决方案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"
    

    你的问题就解决了。

    另见

    【讨论】:

    • 嗨@kjhughes,我根据你的建议进行了更改,但由于某种原因它找不到架构,但我得到了这个:schema_reference.4: Failed to read schema document 'fieldsMapper.xsd' , 因为 1) 找不到文档; 2) 文件无法读取; 3)文档的根元素不是.
    • @F.K.,查看我添加的链接以了解有关如何解决 schemaLocation 问题的更多信息。
    • 我认为问题可能与 Spring Boot 而不是 xsd 或 xml 架构有关,一如既往,您一直很有帮助。我从你身上学到了很多。我会发布我发现的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多