Next: , Previous: , Up: Records   [Contents]


2.3 Slot Naming

As discussed in Record Header, slots are identified by their indexes. Naming is an optional (and recommended) convenience to provide human-readable, intuitive names for slots, irrespective of their index/ordering.

A slot name is a QName that maps to a slot.

A word of caution: the asymptotic complexity of a slot name index lookup is dependent on how your XSLT engine handles dynamic attribute querying. Unless you’re creating records with many dozens of named slots, you probably don’t have to worry about access times.

function: element( R:Record ) R:name-slots (Record as element( R:Record ), qnames as xs:QName*)

xmlns:R="http://mikegerwitz.com/hoxsl/record"

Create a new record header assigning QNames qnames for each slot, in order.

This function is guaranteed to succeed, so callers are responsible for implementing integrity checks: If the length N of names is greater than the number M of slots in Record, then only the first N slot names will be used. If N<M, then slots with an index greater than N will be left unnamed.

This will not remove any existing slot names! If a name conflicts with an existing slot name, it will be overwritten. If a slot is already assigned a name, then both names will be valid slot identifiers.

Definition:

<function name="R:name-slots"  as="element( R:Record )">
  <param name="Record"  as="element( R:Record )" />
  <param name="qnames"  as="xs:QName*" />

  <variable name="usable-count"  as="xs:integer"  select="min( ( count( $qnames ), R:slot-count( $Record) ) )" />

  <variable name="attrs"  as="attribute()*"  select="for $i in 1 to $usable-count return n:attr( $qnames[ $i ], $i )" />

  <variable name="rchildren"  as="node()"  select="$Record/node() except $Record/_R:slot-names" />

  <sequence select="n:element( node-name( $Record ), $Record/@*, ( n:add-attributes( $Record/_R:slot-names, $attrs ), $rchildren ) )" />
</function>

Since slot names are QNames, private slots (like private fields in classical object-oriented programming) can be created by using a namespace that is not likely to be used by anything else. Of course, nothing will prevent the inspection of the record to determine what those namespaces are.

Slot names can be discovered (a form of reflection) using R:slot-names#2. To enforce namespace restrictions, only slot names under the provided namespace will be returned.

function: attribute()* R:slot-names (Record as element( R:Record ), ns as xs:anyURI)

xmlns:R="http://mikegerwitz.com/hoxsl/record"

Retrieve all slot names of Record and their associated slots under the namespace ns.

Slots will be returned as attributes, providing a qname-slot pair. It is possible for multiple slot names to map to the same slot.

Definition:

<function name="R:slot-names"  as="attribute()*">
  <param name="Record"  as="element( R:Record )" />
  <param name="ns"  as="xs:anyURI" />

  <sequence select="$Record/_R:slot-names/@*[ namespace-uri() = $ns ]" />
</function>

Next: , Previous: , Up: Records   [Contents]