タイトル: Description Resource Path Location Type cvc-complex-type.2.4.a: Invalid content was found starting with element 'X'
SEOタイトル: Eclipse「cvc-complex-type.2.4.a Invalid content」XML スキーマ検証エラーの原因と対処
| この記事の要点 |
|
このエラーの典型
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>
対処手順
- エラーメッセージの
One of {...} is expected部分を見る — そこに期待される候補要素が列挙されている - XSD を直接開いて
sequenceの順序を確認 - 該当要素を正しい位置に移動
- Project → Maven → Update Project (Alt+F5) で再検証
- それでも消えなければ Eclipse 再起動、または
.project/.settingsを作り直し
最終手段: XML 検証を OFF
誤検知や Eclipse のキャッシュ問題で消えないことがあります。リファクタを止めないために一時的に検証を切る方法:
- Project を右クリック → Properties
- Validation → Enable project specific settings
- XML Validator の Build / Manual 両方のチェックを外す
- 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 = 想定外 + 必須欠け。