![]() |
![]() |
|
|||
|
|||
|
: | new(), name(), type(), namespace(), scope(), constraint(), default(), fixed(), instance() |
use XML::Schema::Attribute; # list of arguments my $attr = XML::Schema::Attribute->new( name => 'foo', type => 'string' ) || die XML::Schema::Attribute->error(); # hash ref of arguments my $attr = XML::Schema::Attribute->new( { name => 'bar', type => XML::Schema::Type::string->new(), scope => $complex_type, default => 20, namespace => 'http://tt2.org/XML/Example.xml', } ) || die XML::Schema::Attribute->error();
This module implements an object class for representing XML
attributes with XML Schema. An attribute is, of course, a
'name="value"
' pair within the starting tag of an
XML element. In the following example, the attributes are named
'id
' and 'name
'.
<user id="lwall" name="Larry Wall"> ... </user>
Attribute are defined as having a specific type which determines what the acceptable values can be. These types are implemented by various objects derived from the XML::Schema::Type::Simple module. There are over 30 inbuilt simple type, defined in the XML::Schema::Type::Builtin module, and which include things like string, integer, float, time, date, and so on. Furthermore, you can extend these basic types to create your own custom simple types by applying additional validation facets.
An attribute can also define default and fixed values for itself. A fixed constraint specifies a value that the attribute must have, if present in a particular instance document. A default value can be specified for attributes that are missing.
An attribute can be defined within a particular scope. Usually, it is within the definition of a complex type, but can also exist within an attribute group. The attribute communicates with its enclosing scope to resolve its type name (e.g. 'string') with a type object. This allows types to effectively be used before they are defined, and also for attributes to be relocated for use in different scope (e.g. as part of an attribute group) and to resolve the correct type in each location.
Constructor method called to create a new attribute object. A
list of 'key => value
' pairs can be
specified as command line arguments, or alternately, a hash
reference can be passed which contains these configuration
values. The method returns a newly instantiated object on
success. On error it returns undef and sets an internal error
message which can be retrieved by calling error()
as a class method.
# list of options my $attr = XML::Schema::Attribute->new( name => 'foo', type => 'string' ) || die XML::Schema::Attribute->error(); # hash ref of options my $attr = XML::Schema::Attribute->new( { name => 'foo', type => 'string', } ) || die XML::Schema::Attribute->error();
The following configuration options may be specifed:
Name | Typical Values | Description |
name
|
'myattr'
|
The name of the attribute. In the XML fragment <foo bar="baz"/>
the attribute name is 'bar '. This item is mandatory.
|
type
|
'typename'
$type
|
The simple type for this attribute specified either by name or as as reference to an XML::Schema::Type::Simple object or subclass thereof. This item is also mandatory. The remaining items are optional. |
constraint
|
[ fixed => 3.14 ]
[ default => 43 ]
|
This option can be used to specify a fixed
constraint for the attribute, asserting that any specified
instance value matches the fixed value. The second use
shows how a default value can be specified which is used
when no specific instance value is provided. Note that
'constrain
' is a valid alias for
'constraint
'.
|
fixed
|
3.14
|
An alternate way to specify a fixed constraint for an attribute. |
default
|
42
|
An alternate way to specify a default value for an attribute. |
scope
|
$scope
|
This configuration item can be used to bind the attribute to a
particular lexical scope. The attribute type()
method is then able to retrieve type objects from type names via
the scope reference.
|
namespace
|
'http://tt2.org/XML/xyz.xml'
|
The optional XML namespace for the attribute. |
annotation
|
'...interesting note...'
|
An optional annotation for the attribute. This is not yet fully supported. |
name()
Returns the attribute name.
my $name = $attribute->name();
type()
Returns the attribute type, as a reference to an object derived
from XML::Schema::Type::Simple. If the
attribute type was specified as a name rather than a direct
reference to a type then the method will first attempt to
fetch the type object matching that name. It does this by
delegation to its enclosing lexical scope which can be specified
by the scope
configuration item.
my $typeobj = $attribute->type();
namespace( )
namespace( $new_namespace )
Returns the attribute namespace as optionally specified by the
namespace
configuration item. Can also be
called with a single argument to set a new namespace value.
my $ns = $attribute->namespace(); $attribute->namespace('http://tt2.org/XML/Example.xml');
scope( )
scope( $new_scope )
Returns the current scope as specified in the
scope
configuation item. Can also be called
with a single argument to define a new scope for the attribute.
This should be a reference to an object which inherits from the
XML::Schema::Scope class.
my $scope = $attribute->scope(); $attribute->scope($new_scope);
constraint( )
constraint( default => $value )
constraint( fixed => $value )
Accessor method which can be used to get or set the current
value constraints. The value constraint is a pair consisting
of a constraint type, which must be one of
'fixed
' or 'default
',
and a value. Returns the pair ($type, $value)
when called without any arguments.
my ($type, $value) = $attribute->constraint();
When called with appropriate ($type, $value)
arguments, the method sets the current
constraint by calling either the default() or fixed() methods.
$attribute->constraint( default => 42 ); $attribute->constraint( fixed => 84 ); # equivalent to $attribute->default(42); $attribute->fixed(84);
Note that constrain()
is a valid alias for
constraint()
.
# equivalent $attribute->constrain( fixed => 42 ); $attribute->constraint( fixed => 42 );
Accessor method which can be used to get or set the current default value constraint. When called with an argument, the method will force the current value constraint to be 'default' and set the value internally.
$attribute->default(42);
Returns the current default value when called without any arguments.
my $default = $attribute->default();
If the current value constraint is not defined or is set to a
type other than 'default' (e.g. if a 'fixed' constraint has
been specified) then the method returns undef and sets an
internal error value which can be retrieved via the
error()
method.
my $default = $attribute->default(); die $attribute->error() unless defined $default;
Accessor method which can be used to get or set the current
fixed value constraint. As per default()
,
this method can be called with or without an argument and in
the latter case may return undef if a fixed constraint type
isn't defined.
$attribute->fixed(84); my $fixed = $attribute->fixed(); die $attribute->error() unless defined $fixed;
This method attempts to create an instance of the attribute.
The text value parsed from an instance document attribute is
passed to the method as an argument, $value
.
This is then validated according to the underlying type of the
attribute and checked against any default
or
fixed
constraints. If all is well then the
attribute is "activated", by calling any actions scheduled
against the type or attribute. The method returns the result
after activation or the regular post-validation result if no
callbacks are scheduled.
my $val = $attribute->instance('hello world') || die $attribute->error();