This chapter describes the optional DOM Level 3 Validation feature. This module provides APIs to guide construction and editing of XML documents. Examples of such guided editing are queries of the nature that combine questions like "what does the grammar allow me to insert/delete here" and "if I insert/delete here, will the document still be valid."
Users may also want to know several levels of details, e.g., all the possible children, those which would be valid given what precedes this point, lists of defined symbols of a given type, in order to help in editing and creating a XML document. Some of these queries would prompt checks and warn you if you're about to conflict with or overwrite such data.
Finally, users would like to validate an edited or newly
constructed document before serializing it or passing it to other
users. To some, they may edit, come up with an invalid document,
then edit again to result in a valid document. During this process,
these APIs can allow the user to check the validity of the document
or sub-tree on demand; and if needed can also require that the
document or sub-tree remain valid during this editing process via
the continuousValidityChecking
flag.
A DOM application can use the hasFeature(feature,
version)
method of the DOMImplementation
interface to determine with parameter values "VAL-DOC"
and "3.0"
, respectively, whether or not these
interfaces are supported by the implementation.
This chapter focuses on the editing aspects used in the XML document editing world and usage of such information.
This section describes the "VAL-DOC" exceptions.
The Validation operations may throw a ExceptionVAL
as
described in their descriptions.
exception ExceptionVAL { unsigned short code; }; // ExceptionVALCode const unsigned short NO_GRAMMAR_AVAILABLE_ERR = 71;
An integer indicating the type of error generated.
NO_GRAMMAR_AVAILABLE_ERR
DocumentEditVAL
related to the node does not have any grammar and
wfValidityCheckLevel
is set to PARTIAL
or
STRICT_VALIDITY_CHECK
.This section contains "Document Editing" methods as described in
the DocumentEditVAL
,
NodeEditVAL
,
ElementEditVAL
,
and CharacterEditVAL
interfaces.
This interface extends the NodeEditVAL
interface with additional methods for document editing. An object
implementing this interface must also implement the
Document
interface.
interface DocumentEditVAL : NodeEditVAL { attribute boolean continuousValidityChecking; NameList getDefinedElementTypes(in DOMString namespaceURI); void validateDocument() raises(ExceptionVAL); };
continuousValidityChecking
of type boolean
true
will result in an exception being thrown, i.e.,
VALIDATION_ERR
, for documents that are invalid at the
time of the call. When set to true, the implementation if free to
raise the VALIDATION_ERR
exception on DOM operations
that would make the document invalid with respect to "partial
validity." If the document is invalid, then this attribute will
remain false
. This attribute is false
by
default.getDefinedElementTypes
namespaceURI
of type
DOMString
namespaceURI
of namespace. For DTDs, this is
NULL.
|
List of all element node names belonging to the element's namespace. |
validateDocument
NO_GRAMMAR_AVAILABLE_ERR: Raised if an error occurs when the grammar is not available for the document. |
This interface is similar to the [DOM Level 3 Core]
Node
interfaces, with methods for guided document
editing.
interface NodeEditVAL { // CheckTypeVAL const unsigned short WF_CHECK = 1; const unsigned short NS_WF_CHECK = 2; const unsigned short PARTIAL_VALIDITY_CHECK = 3; const unsigned short STRICT_VALIDITY_CHECK = 4; readonly attribute DOMString defaultValue; readonly attribute DOMStringList enumeratedValues; boolean canInsertBefore(in Node newChild, in Node refChild); boolean canRemoveChild(in Node oldChild); boolean canReplaceChild(in Node newChild, in Node oldChild); boolean canAppendChild(in Node newChild); boolean isNodeValid(in boolean deep, in unsigned short wFValidityCheckLevel) raises(ExceptionVAL); };
An integer indicating which type of validation this is. Stricter
validation for certain XML dialects can be done by extending the
NodeEditVAL
interface adding a new
CheckTypeVAL
constant(s).
NS_WF_CHECK
WF_CHECK
.PARTIAL_VALIDITY_CHECK
NS_WF_CHECK
.STRICT_VALIDITY_CHECK
NS_WF_CHECK
.WF_CHECK
defaultValue
of type
DOMString
, readonlyenumeratedValues
of type
DOMStringList
, readonlyDOMStringList
of distinct values for an
attribute or an element declaration.canAppendChild
Node.appendChild
. Determines whether the
Node.replaceChild
operation would make this document
not partially
valid with respect to the grammar.
newChild
of type
Node
Node
to be appended.
|
|
canInsertBefore
Node.insertBefore
operation would make this document
not partially
valid with respect to the grammar.
newChild
of type
Node
Node
to be inserted.refChild
of type
Node
Node
.
|
|
canRemoveChild
Node.removeChild
. Determines whether the
Node.removeChild
operation would make this document
not partially
valid with respect to the grammar.
oldChild
of type
Node
Node
to be removed.
|
|
canReplaceChild
Node.replaceChild
. Determines whether the
Node.replaceChild
operation would make this document
not partially
valid with respect to the grammar.
newChild
of type
Node
Node
.oldChild
of type
Node
Node
to be replaced.
|
|
isNodeValid
deep
of type
boolean
deep
flag on causes the
isNodeValid
method to check for the whole subtree of
the current node for validity. Setting it to false
only checks the current node and its immediate child nodes. The
validateDocument
method on the
DocumentVAL
interface, however, checks to determine
whether the entire document is valid.wFValidityCheckLevel
of
type unsigned short
|
|
NO_GRAMMAR_AVAILABLE_ERR: Exception is raised if the
DocumentEditVAL related to this node does not have any grammar
associated with it and |
This interface extends the NodeEditVAL
interface with additional methods for guided document editing. An
object implementing this interface must also implement
Element
interface.
interface ElementEditVAL : NodeEditVAL { readonly attribute NameList allowedChildren; readonly attribute NameList allowedParents; readonly attribute NameList allowedNextSiblings; readonly attribute NameList allowedPreviousSiblings; readonly attribute NameList allowedAttributes; readonly attribute NameList requiredAttributes; unsigned short contentType(); boolean canSetAttribute(in DOMString attrname, in DOMString attrval); boolean canSetAttributeNode(in Attr attrNode); boolean canSetAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName, in DOMString value); boolean canRemoveAttribute(in DOMString attrname); boolean canRemoveAttributeNS(in DOMString namespaceURI, in DOMString localName); boolean canRemoveAttributeNode(in Node attrNode); boolean isElementDefined(in DOMString name); boolean isElementDefinedNS(in DOMString namespaceURI, in DOMString name); };
allowedAttributes
of
type NameList
, readonlyNameList
of possible Attr
nodes
that can appear with this type of element. Given the names, nodes
can be created from them; note that these are not nodes from the
instance document, but rather are new nodes that could be inserted
in the document.allowedChildren
of type
NameList
, readonlyNameList
of possible Element
nodes
that can appear as children of this type of element. Note that if
no context of this element exists, then this is NULL
;
it is an empty list if the element is not in the document tree.
Given the names, nodes can be created from them; note that these
are not nodes from the instance document, but rather are new nodes
that could be inserted in the document.allowedNextSiblings
of type NameList
, readonlyNameList
of possible sibling
Element
nodes that can appear after this
element.allowedParents
of type
NameList
, readonlyNameList
of possible Element
nodes
that can appear as a parent of this type of element. Note that if
no context of this element exists, for example, the parent element
of this element, then this is NULL
; it is an empty
list if the element is not in the document tree. Given the names,
nodes can be created from them; note that these are not nodes from
the instance document, but rather are new nodes that could be
inserted in the document.allowedPreviousSiblings
of type NameList
, readonlyNameList
of possible sibling
Element
nodes that can appear before this
element.requiredAttributes
of type NameList
, readonlyNameList
of required Attr
nodes
that must appear with this type of element. Given the names, nodes
can be created from them; note that these are not nodes from the
instance document, but rather are new nodes that could be inserted
in the document.canRemoveAttribute
attrname
of type
DOMString
|
|
canRemoveAttributeNS
namespaceURI
of type
DOMString
localName
of type
DOMString
|
|
canRemoveAttributeNode
attrNode
of type
Node
Attr
node to remove from the attribute
list.
|
|
canSetAttribute
attrname
of type
DOMString
attrval
of type
DOMString
|
|
canSetAttributeNS
setAttributeNS
.
namespaceURI
of type
DOMString
namespaceURI
of namespace.qualifiedName
of type
DOMString
value
of type
DOMString
|
|
canSetAttributeNode
attrNode
of type
Attr
Node
in which the attribute can possibly be
set.
|
|
contentType
|
Constant for one of |
isElementDefined
name
is defined in
the grammar.
name
of type
DOMString
|
A boolean that is |
isElementDefinedNS
name
in this
namespace is defined in the current context.
namespaceURI
of type
DOMString
namespaceURI
of namespace.name
of type
DOMString
|
A boolean that is |
This interface extends the NodeEditVAL
interface with additional methods for document editing. An object
implementing this interface must also implement
CharacterData
interface.
interface CharacterDataEditVAL : NodeEditVAL { readonly attribute boolean isWhitespaceOnly; boolean canSetData(in DOMString arg); boolean canAppendData(in DOMString arg); boolean canReplaceData(in unsigned long offset, in unsigned long count, in DOMString arg); boolean canInsertData(in unsigned long offset, in DOMString arg); boolean canDeleteData(in unsigned long offset, in unsigned long count); };
isWhitespaceOnly
of type boolean
, readonlytrue
if content only whitespace;
false
for non-whitespace.canAppendData
arg
of type
DOMString
|
|
canDeleteData
offset
of type
unsigned long
count
of type
unsigned long
|
|
canInsertData
offset
of type
unsigned long
arg
of type
DOMString
|
|
canReplaceData
offset
of type
unsigned long
count
of type
unsigned long
arg
of type
DOMString
|
|
canSetData
arg
of type
DOMString
|
|
This section contains Range-related "Document-editing" methods.
This interface extends the Range
interface with
additional methods for guided document editing.
interface RangeVAL : Range { boolean canSurroundContents(in Node node1, in Node node2, in Node b); NameList getAlternativeElements(in Node refChild); };
canSurroundContents
node1
of type
Node
node2
of type
Node
b
of type
Node
|
|
getAlternativeElements
refChild
of type
Node
|
A list of names of alternative elements. |