私は、ユーザーが JSP ファイルに本質的に似ている XHTML 風のドキュメントを作成するのに役立つツールに取り組んでいます。ドキュメントは XML であり、XHTML 名前空間の適切な形式のタグを含めることができます。それらの間には、私の製品の名前空間の要素が織り込まれています。特に、このツールは XSD を使用して入力を検証します。
入力例:
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<c:paragraph>
<span>This is a test!</span>
<a href="http://www.google.com/">click here for more!</a>
</c:paragraph>
</c:section>
</html>
</markup>
私の問題は、要素のネストの深さに応じて、XSD 検証が一貫して動作しないことです。私が望むのは、名前空間内のすべての要素がhttps://my_tag_lib.example.com/
スキーマに対してチェックされ、名前空間内のすべての要素http://www.w3.org/1999/xhtml
が自由に許容されることです。私の XSD で許可されているすべての HTML 要素をリストするのではなく、特定のブラウザーなどでのみ使用できる不明瞭な要素をユーザーが使用したい場合があります。代わりに、名前空間に属する要素を<xs:any>
.
私が発見したのは、状況によっては、my_tag_lib
名前空間に属しているがスキーマに表示されていない要素は検証に合格しているのに対し、スキーマに表示されている他の要素は無効な属性を与えることで失敗させることができるということです。
* 有効な要素は XSD スキーマに対して検証されます * 無効な要素はバリデータによってスキップされますか?
たとえば、これは検証に合格します。
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</div>
</c:section>
</html>
</markup>
しかし、これは検証に失敗します:
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:paragraph my-invalid-attr="true">This is a test</c:paragraph>
</div>
</c:section>
</html>
</markup>
認識されていない要素がまったくサニタイズされていないように見えるのに、認識された要素のスキーマに対して属性が検証されているのはなぜですか? ここでのロジックは何ですか?私はxmllint
検証を行うために使用しています:
xmllint --schema markup.xsd example.xml
ここに私のXSDファイルがあります:
ファイル:markup.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xs:import namespace="http://www.w3.org/1999/xhtml" schemaLocation="html.xsd" />
<xs:element name="markup">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="xhtml:html" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
ファイル:html.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3.org/1999/xhtml">
<xs:import namespace="https://my_tag_lib.example.com/" schemaLocation="my_tag_lib.xsd" />
<xs:element name="html">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax" namespace="http://www.w3.org/1999/xhtml" />
<xs:any processContents="strict" namespace="https://my_tag_lib.example.com/" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
ファイル:my_tag_lib.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://my_tag_lib.example.com/">
<xs:element name="section">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax" namespace="http://www.w3.org/1999/xhtml" />
<xs:any processContents="strict" namespace="https://my_tag_lib.example.com/" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="paragraph">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax" namespace="http://www.w3.org/1999/xhtml" />
<xs:any processContents="strict" namespace="https://my_tag_lib.example.com/" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>