11
13

More than 3 years have passed since last update.

Java Servlet 4.0のweb.xmlのスキーマ定義

Last updated at Posted at 2019-03-01

Java Servlet 4.0のスキーマ定義が覚えられないのでメモ。
公式の定義がどこに載っているのかわからなかった。

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"                  
         version="4.0">
</web-app>

ついでにスキーマ定義の意味もわかっていなかったので調べてみた

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

「名前空間の名前」を定義する。
これでweb.xmlで使っているタグ要素とかがどの名前空間に属しているかが定義される。

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

(一つ宣言を飛ばして)
ここで先に定義した名前空間http://xmlns.jcp.org/xml/ns/javaeeのスキーマ定義ファイル(.xsd)の参照先を指定している。

これは、「<web-app> とか <servlet> などのエレメントは、名前空間http://xmlns.jcp.org/xml/ns/javaeeに属していて、その定義内容はhttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdの定義ファイルを参照してね。」って意味と理解しました。
ちなみにweb-app_4_0.xsdが、Java Servlet 4.0を指していると思われる。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

schemaLocation が属している名前空間を宣言している。
schemaLocation は、名前空間 の 定義ファイルの場所を参照するために使っている。

まとめ

web.xmlでは2つの名前空間を定義しており、それぞれの用途は以下の通りという解釈。

  1. エレメント(タグ)の名前空間は、http://xmlns.jcp.org/xml/ns/javaeeに属している。
  2. 名前空間の定義ファイルは、 http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
  3. 名前空間と定義ファイルの紐付ける為にschemaLocation の名前空間も宣言している 

参考

web.xmlのバージョン別DTD・XSDの宣言方法
XMLにおける名前空間

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13