XML-Schema

 view release on metacpan or  search on metacpan

docs/xml/modules/XML/Schema/Attribute.xml  view on Meta::CPAN


# 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();
</perl>
  </synopsis>

  <description>
    <p>
    This module implements an object class for representing XML
    attributes with XML Schema.  An attribute is, of course, a
    '<xcode>name="value"</xcode>' pair within the starting tag of an
    XML element.  In the following example, the attributes are named
    '<xcode>id</xcode>' and '<xcode>name</xcode>'.
    </p>

<markup>
<user id="lwall" name="Larry Wall">
    ...
</user>
</markup>
  
    <p>
    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
    <module>XML::Schema::Type::Simple</module> module.  There are over
    30 inbuilt simple type, defined in the
    <module>XML::Schema::Type::Builtin</module> 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 <module
    class="XML::Schema::Facet">validation facets</module>. 
    </p>

    <p>
    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.
    </p>

    <p>
    An attribute can be defined within a particular scope.  Usually,
    it is within the definition of a <module
    class="XML::Schema::Type::Complex">complex type</module>, but can 
    also exist within an <module
    class="XML::Schema::Attribute::Group">attribute group</module>.
    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.
    </p>
    
  </description>

  <methods>
    <method id="new">
      <p>
        Constructor method called to create a new attribute object.  A
        list of '<perlcode>key =&gt; value</perlcode>' 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 <method
        class="XML::Schema::Base">error()</method> as a class method.

<perl>
# 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();
</perl>
      </p>

      <p>
        The following configuration options may be specifed:
      </p>

      <config>
        <item key="name" value="'myattr'">
	    The name of the attribute.  In the XML fragment <xcode><foo bar="baz"/></xcode>
            the attribute name is '<xcode>bar</xcode>'.  This item is mandatory.
        </item>

        <item key="type">
            <value>'typename'</value>
            <value><module class="XML::Schema::Type::Simple">$type</module></value>
	    The simple type for this attribute specified either by name or as 
	    as reference to an <module>XML::Schema::Type::Simple</module> object
	    or subclass thereof.  This item is also mandatory.  The remaining items
	    are optional.
        </item>

        <item key="constraint">
            <value>[ fixed => 3.14 ]</value> 
            <value>[ default => 43 ]</value>
            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
            '<perlcode>constrain</perlcode>' is a valid alias for
            '<perlcode>constraint</perlcode>'.



( run in 2.255 seconds using v1.01-cache-2.11-cpan-5511b514fd6 )