2009-07-08 17 views
3

私は人間とマシンの両方で使用可能な大規模なXML構造を管理するためにXML Includesを使用しようとしています。Xスキーマ/名前空間の検証を含める?

しかし、既存のスキーマに対して検証できるXMLファイルを作成しようとすると、無数の問題が発生します。ここに私がしようとしているものの単純な例があります。

私の "main.xml"ファイルは検証されません。

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Main.xml - This fails to validate. --> 
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd" 
      xmlns:ns1="http://www.example.com/main" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xi="http://www.w3.org/2001/XInclude"> 

    <name>String</name> 
    <xi:include href="child.xml"/> <!-- What I'm trying to do. --> 

</ns1:main> 

"child.xml"ファイルは、スタンドアロンファイルとしてfineを検証します。

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Child.xml - This validates fine, as a standalone file. --> 
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd" 
      xmlns:ns1="http://www.example.com/main" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <name>String</name> 
    <age>String</age> 

</ns1:child> 

は、ここに私のスキーマです:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Main.xsd - My Simplified Schema --> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:ns1="http://www.example.com/main" 
      targetNamespace="http://www.example.com/main"> 

    <!-- Main Element (References Child) --> 
    <xs:element name="main"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element ref="ns1:child"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <!-- Child Element --> 
    <xs:element name="child"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element name="age" type="xs:string"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

私の問題はほとんど-明らかに名前空間に関連しているが、私は私の問題を解決する方法のための損失でね。

+0

妥当性検査をしようとすると、どのようなエラーが発生しますか? – skaffman

+0

main.xmlの解析時に、次のエラーが表示されます。「 'schemaLocation'属性は、ターゲットの名前空間がすでに検証に使用されているスキーマを参照しています。 - AltovaのXML Spyで。 – Nate

+0

Notepad ++のXML検証プラグインでは、「Element '{http://www.w3.org/2001/XInclude〕include」:この要素は期待されていません。({http://www.example.com/main}子 )。" – Nate

答えて

5

skaffman既に指摘されているように、XMLスキーマとXIncludeは互換性がありません。

xmllint状態明確このから検証エラーメッセージ:W3C Recommendationを引用する

main.xml:9: element include: Schemas validity error : Element '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ({http://www.example.com/main}child). 
main.xml fails to validate 

「XIncludeのXMLスキーマを適用することによって生成される拡張インフォセットには何の関係を定義していないような増補情報セットとして供給することができます。入力infoset、またはそのような増強は、包含から生じるinfosetに適用されるかもしれません。

したがって、まずXIncludesを適用してXMLファイル全体を構築し、その後このファイルを検証する必要があります。

編集:xmllintに--xincludeを指定すると、main.xmlを検証できます。

+0

--xincludeフラグが私のために働いた、ありがとう – wulfgarpro

2

XMLスキーマとXIncludeの仕様が互いに互換性があるとは限りません。個々のXMLプロセッサはそれを可能にするかもしれないが、他のものはそうしないだろう。

一般的に、2つを一緒に使用しないでください。

P.S.なぜこれが名前空間の問題だと思うのか分かりません。その印象を与えるものは何ですか?

0

これは、XIncludeとschemaの間に互換性がないために発生しているとは思われません。インクルードが検証前に処理されていないように見えます。したがって、スキーマでは、メインの "include"要素、 "子"要素のみが許可されていません。あなたのXMLプロセッサを強制的に処理する場合は、検証の前にインクルードしてください。

2

私はgrantwparksに同意します - XIncludeとXMLスキーマは絶対に一緒に使用できます。仕様は意図的に互いに独立しています。明らかに、XIncludeの著者は、検証の前または検証後にインクルードを処理する自由を提供することを望んでいました。

このthread on velocityreviewsは、問題を説明し、私は問題を理解するために助けた答えはそれから引用し、this post on xml.comた:

One of the most common questions about XInclude is how inclusion interacts with validation, XSL transformation, and other processes that may be applied to an XML document. The short answer is that it doesn't. XInclusion is not part of any other XML process. It is a separate step which you may or may not perform when and where it is useful to you.

For example, consider validation against a schema. A document can be validated before inclusion, after inclusion, or both. If you validate the document before the xi:include elements are replaced, then the schema has to declare the xi:include elements just like it would declare any other element. If you validate the document after the xi:include elements are replaced, then the schema has to declare the replacement elements. Inclusion and validation are separate, orthogonal processes that can be performed in any order which is convenient in the local environment

それが煮詰めるようだだから何、プロセスにあなたのXMLツールを取得することです検証の前にxi:include要素(つまり、OPの例が必要とするもの)。たとえば、EclipseのXMLエディタでは、XML - > XMLファイル - >検証(RSA 8.5を使用)の下に "Process XML Inclusions"という設定があります。これは、xi:include beforeを処理するエディタを起動する必要があります検証。

アンディ

関連する問題