XML Declaration

An XML document may have a an XML declaration on the first line.

<?"xml version="1.0" {encoding="Encoding"} {standalone="yes" | "no"} ?>

The XML declaration has the same markers as processing instructions <? …?> and is sometimes referred to as a process instruction, but formally it is not.

When an XML declaration is present in a document, the version attributes is required; the encoding and standalone attributes are optional.

The XML declaration is added by the software that writes the document and is used by the software that reads the document. It specifies how the XML was serialized but has little or no meaning for the document in memory. When it is re-serialized, this information is added again by the generating software.

Conversion to and from Structs

To convert an XML declarations to Structs, use the xmlToStruct/full ProcScript statement. The XML declaration is not converted to a Struct itself; instead, it is converted to annotations on the un-named top-level Struct:

  • xmlVersion = 1.0
  • xmlEncoding = Encoding
  • xmlStandAlone = yes | no

When converted without the /full switch, XML declarations are ignored.

To generate an XML declaration from a Struct, set the xmlVersion tag on the top-level Struct, and optionally the xmlEncoding and xmlStandAlone tags, before calling structToXml.

XML to Struct Conversion

XML document with declaration:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p/>

After conversion with /full, string returned by $dbgString:

[]
  [$tags]
    [xmlVersion] = 1.0
    [xmlEncoding] = UTF-8
    [xmlStandAlone] = yes
  [p] = ""
    [$tags]
      [xmlClass] = element

The information of the XML declaration can only be accessed using the $tags Struct function:

vVersion = vStruct->$tags->xmlVersion        ; Result: vVersion = "1.0"
vEncoding = vStruct->$tags->xmlEncoding      ; Result: vEncoding = "UTF-8"
vStandAlone = vStruct->$tags->xmlStandAlone  ; Result: vStandAlone $1 = "yes"

Struct to XML Conversion:

The following code prepares a Struct with one member, and sets the xmlVersion attribute on the top-level Struct:

vStruct = $newstruct
vStruct->$tags->xmlVersion="1.0"
vStruct->p = "text"

The resulting XML looks like this:

<?xml version="1.0"?>
<p>text</p>

Related Topics