# saxon:doctype

The `saxon:doctype` instruction is used to insert a document type declaration into the current output file. It should be instantiated before the first element in the output file is written. It must be used only when writing a final result tree (not a temporary tree) and only when writing text nodes. The reason for these restrictions is that saxon:doctype writes directly to the serialized output stream (internally it uses disable-output-escaping to achieve this). It is not possible to represent a doctype declaration as a node on a temporary tree.

The `saxon:doctype` instruction takes no attributes. The content of the element is a template-body that is instantiated to create an XML document that represents the DTD to be generated; this XML document is then serialized using a special output method that produces DTD syntax rather than XML syntax.

If this element is present the doctype-system and doctype-public attributes of `xsl:output` should not be present.

The generated XML document uses the following elements, where the namespace prefix "dtd" is used for the namespace URI "http://saxon.sf.net/dtd":

dtd:doctype | Represents the document type declaration. This is always the top-level element. The element may contain dtd:element, dtd:attlist, dtd:entity, and dtd:notation elements. It may have the following attributes: name (mandatory) The name of the document type system The system ID public The public ID
dtd:element | Represents an element type declaration. This is always a child of dtd:doctype. The element is always empty. It may have the following attributes: name (mandatory) The name of the element type content (mandatory) The content model, exactly as it appears in a DTD, for example content="(#PCDATA)" or content="( a | b | c)*"
dtd:attlist | Represents an attribute list declaration. This is always a child of dtd:doctype. The element will generally have one or more dtd:attribute children. It may have the following attributes: element (mandatory) The name of the element type

dtd:attribute

>Represents an attribute declaration within an attribute list. This is always a child of dtd:attlist. The element will always be empty. It may have the following attributes:
name (mandatory) The name of the attribute
type (mandatory) The type of the attribute, exactly as it appears in a DTD, for example type="ID" or type="( red | green | blue)"
value (mandatory) The default value of the attribute, exactly as it appears in a DTD, for example value="#REQUIRED" or value="#FIXED 'blue'"

dtd:entity

Represents an entity declaration. This is always a child of dtd:doctype. The element may be empty, or it may have content. The content is a template body, which is instantiated to define the value of an internal parsed entity. Note that this value includes the delimiting quotes. The xsl:entity element may have the following attributes:
name (mandatory) The name of the entity
system The system identifier
public The public identifier
parameter Set to "yes" for a parameter entity
notation The name of a notation, for an unparsed entity

dtd:notation | Represents a notation declaration. This is always a child of dtd:doctype. The element will always be empty. It may have the following attributes: name (mandatory) The name of the notation system The system identifier public The public identifier

Note that Saxon will perform only minimal validation on the DTD being generated; it will output the components requested but will not check that this generates well-formed XML, let alone that the output document instance is valid according to this DTD.

Example:

    <xsl:template match="/">
      <saxon:doctype xsl:extension-element-prefixes="saxon">
      <dtd:doctype name="booklist"
            xmlns:dtd="http://saxon.sf.net/dtd" xsl:exclude-result-prefixes="dtd">
        <dtd:element name="booklist" content="(book)*"/>
        <dtd:element name="book" content="EMPTY"/>
        <dtd:attlist element="book">
          <dtd:attribute name="isbn" type="ID" value="#REQUIRED"/>
          <dtd:attribute name="title" type="CDATA" value="#IMPLIED"/>
        </dtd:attlist>
        <dtd:entity name="blurb">'A <i>cool</i> book with &gt; 200 pictures!'</dtd:entity>
        <dtd:entity name="cover" system="cover.gif" notation="GIF"/>
        <dtd:notation name="GIF" system="http://gif.org/"/>
      </dtd:doctype>
      </saxon:doctype>
      <xsl:apply-templates/>
    </xsl:template>

Although not shown in this example, there is nothing to stop the DTD being generated as the output of a transformation, using instructions such as `xsl:value-of` and `xsl:call-template`. It is also possible to use `xsl:text` with `disable-output-escaping="yes"` to output DTD constructs not covered by this syntax, for example conditional sections and references to parameter entities.

Expand | Up Next