XML Well-Formed

No Gravatar

Perché mai un post sulla “formattazione” corretta di un documento XML?Flow of data in a typical parser

Perché alle volte è bene avere presenti questi particolari, soprattutto per non farsi male quando si vanno ad utilizzare parser vari ed eventuali..

Un documento XML si dice well-formed (ed è quindi analizzabile da un parser) se:

  • Non-empty elements are delimited by both a start-tag and an end-tag.
  • Empty elements may be marked with an empty-element (self-closing) tag, such as <IAmEmpty />. This is equal to <IAmEmpty></IAmEmpty>.
  • All attribute values are quoted with either single (‘) or double (“) quotes. Single quotes close a single quote and double quotes close a double quote.
  • Tags may be nested but must not overlap. Each non-root element must be completely contained in another element.
  • The document complies with its declared character encoding. The encoding may be declared or implied externally, such as in “Content-Type” headers when a document is transported via HTTP, or internally, using explicit markup at the very beginning of the document. When no such declaration exists, a Unicode encoding is assumed, as defined by a Unicode Byte Order Mark before the document’s first character. If the mark does not exist, UTF-8 encoding is assumed.
  • Element names are case-sensitive.

(Tratto da Wikipedia)

Ovviamente se volete far leggere un documento XML non well-formed ad un qualsivoglia Parser, sia questo SAX o DOM, otterrete un errore. Per ovviare a ciò potete:

  1. Introdurre delle sequenze di escape per gli elementi,
  2. Modificare il documento (o crearne uno nuovo) strutturandolo in modo corretto.

Che io sappia non ci sono altre alternative (se ne avete segnalatemele che fanno sempre comodo) e per interpretare documenti strutturati non correttamente si deve passare necessariamente da una delle 2 soluzioni.

Un unico dubbio mi assilla (esageriamo via..) ma una pagina jsp che può contenere tag del tipo

<%= variabile_di_tipo_string >

come verrà parsato? Userà uno dei due metodi precedenti o si saranno riscritti un parser che non necessita la proprietà di buona formattazione? Magari avranno usato un generatore di parser a partire da grammatiche (javacc e simili..)..

Vabbè al momento non è il mio problema principale :)

Molto presto credo che parlerò di XSLT.. stay tuned!

Popularity: 19% [?]

Tags: , , , , , , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply