シーケンス型の使用
シーケンス型は、関数の戻りの型またはパラメーターの型に関する 変数を宣言する場合に使用できます。
このタスクについて
XML API では、シーケンス型は XSequenceType オブジェクトを 使用して表されます。XSequenceType オブジェクトは、XPath 2.0 で定義された SequenceType 構文と 同じシーケンス型のセットを表すことができます。XSequenceType オブジェクトは、XSequenceTypeFactory インターフェースを 通じて作成されます。XSequenceTypeFactory のインスタンスは、XFactory インスタンスで getSequenceTypeFactory() メソッドを 使用して取得できます。
手順
例
メソッド・シグニチャー | シーケンス型の例 | コメント |
---|---|---|
emptySequence() | empty-sequence() | |
item(OccurrenceIndicator のカーディナリティー) | item()* | |
atomic(QName typeName, OccurrenceIndicator のカーディナリティー) | xs:integer | "sc:type" は、ユーザー定義のスキーマ型を参照する QName です。 |
sc:type+ | ||
documentNode(OccurrenceIndicator のカーディナリティー) | document-node()? | |
documentNodeWithElement(QName elementNameOrWildcard, QName typeName, boolean nillable, OccurrenceIndicator のカーディナリティー) | document-node(element()) | "ns:elem" はエレメント名を表す QName、"sc:type" はユーザー定義のスキーマ型を参照する QName です。
elementNameOrWildcard パラメーターと typeName パラメーターはオプションです。 エレメント名または型を問わない場合は、ヌルを使用します。 |
document-node(element(ns:elem))? | ||
document-node(element(*, sc:type?)) | ||
documentNodeWithSchemaElement(QName elementName, OccurrenceIndicator のカーディナリティー) | document-node(schema-element(sc:elemDecl)) | "sc:elemDecl" は、スキーマ内のグローバル・エレメント宣言を参照する QName です。 |
element(QName elementNameOrWildcard, QName typeName, boolean nillable, OccurrenceIndicator のカーディナリティー) | element(*) | "ns:elem" はエレメント名を表す QName、"sc:type" はユーザー定義のスキーマ型を参照する QName です。
elementNameOrWildcard パラメーターと typeName パラメーターはオプションです。 エレメント名または型を問わない場合は、ヌルを使用します。 |
element(ns:elem, sc:type)* | ||
attribute(QName attributeNameOrWildcard, QName typeName, OccurrenceIndicator のカーディナリティー) | attribute()+ | "ns:attrib" は、属性名を表す QName です。
attributeNameOrWildcard パラメーターと typeName パラメーターはオプションです。 属性名または型を問わない場合は、ヌルを使用します。 |
attribute(ns:attrib) | ||
attribute(ns:attrib, xs:string)? | ||
schemaElement(QName elementName, OccurrenceIndicator のカーディナリティー) | schema-element(sc:elemDecl)* | "sc:elemDecl" は、スキーマ内のグローバル・エレメント宣言を参照する QName です。 |
schemaAttribute(QName attributeName, OccurrenceIndicator のカーディナリティー) | schema-attribute(sc:attribDecl) | "sc:attribDecl" は、スキーマ内のグローバル属性宣言を参照する QName です。 |
processingInstruction(QName piNCName, OccurrenceIndicator のカーディナリティー) | processing-instruction()? | "pinst" は、処理命令の名前を表す NCName です。 |
processing-instruction("pinst") | ||
processing-instruction(pinst) | ||
comment(OccurrenceIndicator のカーディナリティー) | comment() | |
text(OccurrenceIndicator のカーディナリティー) | text()+ | |
node(OccurrenceIndicator のカーディナリティー) | node()* |
以下の例で、さまざまなシーケンス型を表す XSequenceType オブジェクトの作成方法を示します。
これらの結果のいくつかは、スキーマからの型と宣言を参照します。
この例では、スキーマが schemaSource ソース・オブジェクトを通じて使用できることを前提としています。
// Create the factory
XFactory factory = XFactory.newInstance();
// Obtain an XSeqeuenceTypeFactory instance
XSequenceTypeFactory stFactory = factory.getSequenceTypeFactory();
// Create a sequence type for a sequence of xs:integer values: "xs:integer*"
XSequenceType integerSequenceType = stFactory.atomic(
XTypeConstants.INTEGER_QNAME,
XSequenceType.OccurrenceIndicator.ZERO_OR_MORE);
// Create a sequence type for a single node: "node()"
XSequenceType nodeType = stFactory.node(OccurrenceIndicator.ONE);
// Define a constant for the target namespace of a schema containing user-defined types and declarations
final String targetNamespace = "http://www.example.org/schema/";
// Register the schema with the XFactory
factory.registerSchema(schemaSource);
// Create a sequence type for exactly one document node with an element of type "employeeRecord" from the schema:
// "document-node(element(*, ns:employeeRecord))"
XSequenceType employeeRecordDocumentType = stFactory.documentNodeWithElement(
null,
new QName(targetNamespace, "employeeRecord"),
false,
XSequenceType.OccurrenceIndicator.ONE);
// Create a sequence type for an optional attribute matching the attribute declaration "type" in the schema:
// "schema-attribute(ns:type)?"
XSequenceType optionalEmployeeType = stFactory.schemaAttribute(
new QName(targetNamespace, "type"),
XSequenceType.OccurrenceIndicator.ZERO_OR_ONE);
// Create a sequence type for one or more atomic values of type "telephoneNumber" from the schema:
// "ns:telephoneNumber+"
XSequenceType telephoneNumbersType = stFactory.atomic(
new QName(targetNamespace, "telephoneNumber"),
XSequenceType.OccurrenceIndicator.ONE_OR_MORE);
以下に、サンプル・スキーマの内容を示します。
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://www.example.org/schema/" targetNamespace="http://www.example.org/schema/">
<complexType name="employeeRecord">
<sequence>
<element name="name" type="string"></element>
<element name="id" type="int"></element>
<element name="telephone" type="ns:telephoneNumber"></element>
</sequence>
<attribute ref="ns:type"></attribute>
</complexType>
<simpleType name="telephoneNumber">
<restriction base="string">
<pattern value="\d{3}-\d{3}-\d{4}"></pattern>
</restriction>
</simpleType>
<element name="employee" type="ns:employeeRecord"></element>
<simpleType name="employeeType">
<restriction base="string">
<enumeration value="full-time"></enumeration>
<enumeration value="part-time"></enumeration>
<enumeration value="seasonal"></enumeration>
</restriction>
</simpleType>
<attribute name="type" type="ns:employeeType"></attribute>
</schema>