What is the type of an attribute with a default or fixed value? Is it always string?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the type of an attribute with a default or fixed value? Is it always string?

Costello, Roger L.
Hi Folks,

In section 3.2.2 of the Structures specification it has a box showing this:

    XML Representation Summary: attribute Element Information Item

   <attribute
         default = string
         fixed = string
         ...

I interpret that to mean the type of a default or fixed value is always string. Yes?

Suppose I declare this:

     <attribute name="pi" type="decimal" default="3.14" />

Suppose I use the default value in an XML instance document:

    pi = "3.14"

Since the value is (presumably) a string type, it is nonsensical to perform arithmetic on it in, say, an XSLT program:

     @pi * diameter

And one should expect an error to be thrown. Yes?

Now, suppose I don't use the default value:

    pi = "3.14159"

Since the declared type of pi is decimal, I can perform arithmetic on it:

     @pi * diameter

Yes?

Recap: sometimes pi is a string (and I can't do arithmetic on it) and sometime pi is a decimal (and I can do arithmetic on it). Yes?

/Roger
 


Reply | Threaded
Open this post in threaded view
|

Re: What is the type of an attribute with a default or fixed value? Is it always string?

Michael Kay

On 20/07/2012 12:19, Costello, Roger L. wrote:

> Hi Folks,
>
> In section 3.2.2 of the Structures specification it has a box showing this:
>
>      XML Representation Summary: attribute Element Information Item
>
>     <attribute
>           default = string
>           fixed = string
>           ...
>
> I interpret that to mean the type of a default or fixed value is always string. Yes?
The type of the @default (or @fixed) attribute in the schema document is
string, yes (though there are semantic rules that constrain it further).
But in the corresponding schema component, the type of the {value}
property of the value constraint is typed, for example as xs:decimal if
the attribute being declared has type xs:decimal.

>
> Suppose I declare this:
>
>       <attribute name="pi" type="decimal" default="3.14" />
>
> Suppose I use the default value in an XML instance document:
>
>      pi = "3.14"
>
> Since the value is (presumably) a string type, it is nonsensical to perform arithmetic on it in, say, an XSLT program:
>
>       @pi * diameter
No, you are confusing the type of the @default attribute in the schema
document with the type of the @pi attribute in the instance document.

Michael Kay
Saxonica


Reply | Threaded
Open this post in threaded view
|

Re: What is the type of an attribute with a default or fixed value? Is it always string?

C. M. Sperberg-McQueen-2
In reply to this post by Costello, Roger L.

On Jul 20, 2012, at 5:19 AM, Costello, Roger L. wrote:

> Hi Folks,
>
> In section 3.2.2 of the Structures specification it has a box showing this:
>
>    XML Representation Summary: attribute Element Information Item
>
>   <attribute
>         default = string
>         fixed = string
>         ...
>
> I interpret that to mean the type of a default or fixed value is always string. Yes?

Not quite.

It means that the type of the attributes named 'default' and 'fixed' on
elements named xsd:attribute appearing in a valid schema document
is string.

The default or fixed value of the attributes being declared by those
xsd:attribute elements, however, is given by the property
{value constraint}.{value} in the schema components constructed
from those xsd:attribute elements.

Sections 3.2.2.1 (http://www.w3.org/TR/xmlschema11-1/#dcl.att.global),
3.2.2.2 (http://www.w3.org/TR/xmlschema11-1/#dcl.att.local), and
3.2.2.3 (http://www.w3.org/TR/xmlschema11-1/#ref.att.local) describe
the component properties and how they are derived from the values
in the schema document.  in 3.2.2.3, for example, the description of
{value constraint}.{value} says that its value is

    the ¬∑actual value¬∑ of the [attribute] (with respect to
    {attribute declaration}.{type definition})

where "the attribute" refers to the default or fixed attribute in the source
declaration, and {attribute declaration}.{type definition} refers to the
type definition of the responsible attribute declaration.  

In less formal language:  the string in the schema document is mapped
to an 'actual value' in the value space of the attribute's declared type; that
value is what is used as the default or fixed value of the attribute.

>
> Suppose I declare this:
>
>     <attribute name="pi" type="decimal" default="3.14" />
>
> Suppose I use the default value in an XML instance document:
>
>    pi = "3.14"

Actually, if this is part of your instance, you haven't used the
default value here, you've specified a value (which happens
to be the same as the default value).

>
> Since the value is (presumably) a string type,

The rules I quote above specify that in the case you describe,
the default value of the 'pi' attribute will be the decimal value
mapped to by the string "3.14", and not the string "3.14"
itself.  If pi were declared as being of type float, the default
value would be the float mapped to by "3.14".  And so on.

> it is nonsensical to perform arithmetic on it in, say, an XSLT program:
>
>     @pi * diameter
>
> And one should expect an error to be thrown. Yes?

That is precisely the consequence that the rules in the spec are
designed to circumvent.  And the kind of question about details like
how the type assignment is performed are precisely the kind of
question that the cumbersome and turgid "XML Mapping Rules"
are designed to answer.  Those rules are extremely pedantic and
verbose, and I expect many readers find them mind-numbling boring.
But anyone who wants to understand XSD at the level of detail
implied by your question needs to learn how to read them.

I hope this helps.


--
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com 
* http://cmsmcq.com/mib                 
* http://balisage.net
****************************************************************