- PR -

流通ビジネスメッセージ標準のXML Schemaによる解析

1
投稿者投稿内容
momo
常連さん
会議室デビュー日: 2006/11/06
投稿数: 35
投稿日時: 2007-09-20 22:34
[環境]
JDK1.5.0_12
JAXP
デフォルトのxerces

流通ビジネスメッセージ標準ver1.0のサンプルXMLと標準スキーマを使用して、SchemaFactoryでバリデーションを行いました。
ですが、

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'sh:StandardBusinessDocument'.
行:11

のエラーが出てしまいます。

ソースは
コード:
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = sf.newSchema( new StreamSource( "C:/hoge/Schemas/SGE_OrderProxy1P.xsd" ) );

Validator validator = schema.newValidator();
validator.validate( new StreamSource(new StringReader(pstrXML)) );


のようにしています。
(pstrXMLの内容は経済産業省から送られてきたサンプルのxmlファイルの文字列が入っています。)
このコードで名前空間のない単純なXSDでの検証は正常に行えました。


また、エラーメッセージにある'sh:StandardBusinessDocument'はxmlのルートタグで、

コード:
<sh:StandardBusinessDocument 
	xmlns:common="urn:SecondGenEDI:common:Japan:1" 
	xmlns:order="urn:SecondGenEDI:order:Japan:1" 
	xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="urn:SecondGenEDI:common:Japan:1 ../Schemas/SGE_OrderProxy1P.xsd http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd ">
・・・


と記述されています。


DOMツリーの作成はうまくできるの、たぶんxsdの名前空間などの解決ができていないのでは?と思うのですが、
標準のXSDは膨大すぎてどこから追っていいのかわかりません。
schemaLocationの指定が悪さをしているのか?と思い削除して上記のコード解析してみましたが
同様のエラーが出てしまいました。

投稿するカテゴリーも迷ったのですが、XMLのほうにしてみました。
どなたかわかりそうな方がいらっしゃいましたら
よろしくお願いします。
kowest
会議室デビュー日: 2005/04/18
投稿数: 5
投稿日時: 2007-09-20 23:40
XML系のプログラムは何年もしていないので的外れかもしれませんが・・・

Javadoc見る限り、
StreamSourceのコンストラクタはFile、Stringなどの引数がありますが
Fileオブジェクトにしてみるか、URI構文に沿ったString型で試してみてください。
momo
常連さん
会議室デビュー日: 2006/11/06
投稿数: 35
投稿日時: 2007-09-21 11:37
ありがとうございます。
以下のようにしてみましたが結果は同様でした。
コード:
File file = new File("C:/hoge/Schemas/SGE_OrderProxy1P.xsd");
System.out.println("スキーマファイル" + file.getPath());

SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = sf.newSchema( new StreamSource( file ));

Validator validator = schema.newValidator();
validator.validate( new StreamSource(new StringReader(pstrXML)) );



知識が浅いながらも落ち着いてxsdを解析してみました。

SGE_OrderProxy1P.xsdが会席に指定すべきTOPのxsdだと思い、このファイルのみをスキーマに指定していました。
が、StandardBusinessDocumentタグが宣言されているStandardBusinessDocumentHeader.xsdをどのxsdファイルも
インクルードしてくれていないことに気がつきました。

解析対象のxmlのStandardBusinessDocumentタブのschemaIncludeでは
StandardBusinessDocumentHeader.xsdを指定しているので、
もしかして、と思い、このxsdもスキーマクラスのコンストラクタに
渡してあげるようにしてみたところ無事にバリデーションできました。

お騒がせしました。


コード:

SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );

File file1 = new File("C:/hoge/Schemas/SGE_OrderProxy1P.xsd");
File file2 = new File("C:/hoge/Schemas/sdbh/StandardBusinessDocumentHeader.xsd");

StreamSource[] sources = new StreamSource[2];
sources[0] = new StreamSource(file1);
sources[2] = new StreamSource(file2);

Schema schema = sf.newSchema( sources );

Validator validator = schema.newValidator();
validator.validate( new StreamSource(new StringReader(pstrXML)) );


ディレクトリ構造
Schema --------------SGE_OrderProxy1P.xsd
    |
    | -----sbdh--------------BasicTypes.xsd
    |                  |-----BusinessScope.xsd
    |                  |-----DocumentIdentification.xsd
    |                  |-----Manifest.xsd
    |                  |-----Partner.xsd
    |                  |-----StandardBusinessDocumentHeader.xsd ←ここに-StandardBusinessDocumentタグの宣言がある
    |-----SecondGenEDI
            |
            |------VIP-------------CommonProxy1.xsd
                    |
                    |-----order--------ListOfOrders.xsd
                    |              |---OrderLineItem.xsd
                    |              |---OrderType.xsd
                    |
                    |----common--------Amount.xsd
                                   |---BalanceCarriedCode.xsd
                                   |---base_decimal.xsd
                                   |---base_kana.xsd
                                         ・
                                         ・
                                         ・


1

スキルアップ/キャリアアップ(JOB@IT)