4.

Eclipse「cvc-complex-type.2.4.a Invalid content」XML スキーマ検証エラーの原因と対処

編集
この記事の要点
  • Eclipse の XML スキーマ検証で pom.xml / web.xml / Spring 設定 XML 等の要素順序が XSD の <xsd:sequence> と不一致
  • メッセージ: cvc-complex-type.2.4.a: Invalid content was found starting with element "X". One of {...} is expected.
  • 原因: XSD は要素順序が厳密。例: pom.xml は modelVersionparentgroupId → ... の順序
  • 対処: XSD 定義を見て正しい順に並べ替え、不要な要素を削除
  • どうしても消えない場合は Project → Properties → Validation で XML 検証を一時 OFF(最終手段)
  • 実行に影響しないことが多い(Maven / 起動は通る)が、IDE のエラーマーカーが消えないとリファクタが滞る

このエラーの典型

Description                              Resource    Path  Location  Type
cvc-complex-type.2.4.a: Invalid content
was found starting with element
'dependencies'. One of '{...build,
...profiles, ...repositories}' is expected.
                                         pom.xml     /demo  line 25   XML Problem

意味: 「dependencies 要素が出てきたが、ここでは build / profiles / repositories のいずれかが期待されている」。XSD で定義された要素順序に違反している、というシグナルです。

原因: XSD の sequence は順序固定

XML Schema (XSD) には <xsd:sequence> という構造があり、子要素を定義した順に書かなければならない制約があります。pom.xml も Spring 設定もこのスキーマで縛られています。

<!-- pom.xml の XSD 定義(抜粋) -->
<xsd:complexType name="Model">
    <xsd:all>
        <xsd:element name="modelVersion" .../>
        <xsd:element name="parent" .../>
        <xsd:element name="groupId" .../>
        <xsd:element name="artifactId" .../>
        <xsd:element name="version" .../>
        <xsd:element name="packaging" .../>
        ...
        <xsd:element name="dependencies" .../>
        <xsd:element name="build" .../>
        ...
    </xsd:all>
</xsd:complexType>

例 1: pom.xml で順序が違う

<!-- ❌ NG: build より後に dependencies がきている -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0</version>

    <build>
        <plugins>...</plugins>
    </build>

    <dependencies>  <!-- ← cvc-complex-type.2.4.a -->
        <dependency>...</dependency>
    </dependencies>
</project>

<!-- ✅ OK: dependencies → build の順 -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>...</dependency>
    </dependencies>

    <build>
        <plugins>...</plugins>
    </build>
</project>

pom.xml の正しい要素順序

要素
1<modelVersion>
2<parent>
3<groupId>, <artifactId>, <version>, <packaging>
4<name>, <description>, <url>
5<properties>
6<dependencyManagement>
7<dependencies>
8<build>
9<profiles>, <repositories> など

例 2: web.xml で順序が違う

<!-- ❌ NG: filter の後に context-param -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="4.0">
    <display-name>app</display-name>
    <filter>...</filter>

    <context-param>  <!-- ← cvc-complex-type.2.4.a -->
        <param-name>x</param-name>
        <param-value>y</param-value>
    </context-param>
</web-app>

<!-- ✅ OK: 正しい順序 -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="4.0">
    <display-name>app</display-name>
    <context-param>...</context-param>
    <filter>...</filter>
    <filter-mapping>...</filter-mapping>
    <listener>...</listener>
    <servlet>...</servlet>
    <servlet-mapping>...</servlet-mapping>
    <session-config>...</session-config>
    <welcome-file-list>...</welcome-file-list>
    <error-page>...</error-page>
</web-app>

例 3: Spring XML で xmlns 不足

名前空間 (xmlns) を宣言していないとその要素が「未定義」扱いされ、同じエラーになります。

<!-- ❌ NG: context: の xmlns 未宣言 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.example"/>
</beans>

<!-- ✅ OK: xmlns:context を宣言 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example"/>
</beans>

対処手順

  1. エラーメッセージの One of {...} is expected 部分を見る — そこに期待される候補要素が列挙されている
  2. XSD を直接開いて sequence の順序を確認
  3. 該当要素を正しい位置に移動
  4. Project → Maven → Update Project (Alt+F5) で再検証
  5. それでも消えなければ Eclipse 再起動、または .project / .settings を作り直し

最終手段: XML 検証を OFF

誤検知や Eclipse のキャッシュ問題で消えないことがあります。リファクタを止めないために一時的に検証を切る方法:

  1. Project を右クリック → Properties
  2. Validation → Enable project specific settings
  3. XML Validator の Build / Manual 両方のチェックを外す
  4. Apply and Close → クリーンビルド

または特定ファイルだけ除外:

<!-- pom.xml で対象ファイルを Validation 除外 -->
<!-- Eclipse Preferences → Validation → XML Validator → Settings →
     Exclude Group で path: **/web.xml 等を追加 -->

FAQ

Q: ビルド・実行は通るのに Eclipse だけエラー
A: Eclipse 内蔵の XML Validator が古い XSD を参照していることがあります。Project → Clean → Rebuild、ダメなら Eclipse 再起動。

Q: pom.xml の順序を自動で正しく並べる
A: IntelliJ IDEA は自動整列機能あり。Eclipse は m2e プラグインで Format XML 実行(順序は変えない)。手動移動が確実。

Q: cvc-complex-type.2.4.b との違い
A: 2.4.a = 想定外の要素、2.4.b = 必須要素が欠けている、2.4.c = 想定外 + 必須欠け。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. createActionFormにてNullPointerException
  2. java.lang.NullPointerException: Module 'null' not found
  3. javax.servlet.jsp.JspException: Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
  4. Description Resource Path Location Type cvc-complex-type.2.4.a: Invalid content was found starting with element 'X'
  5. パス ... に対するアクションのインスタンスがありません
  6. TLDによると、タグ form の属性 id は無効です

最近更新/作成されたページ