Tenim dades que s'han de emmagatzemar o transmetre. Les podem tenir:
L'elecció dependrà de l'estructura i l'accés que requereixen les dades. Per exemple: els fitxers són molt senzills d'implementar, però si hi ha moltes dades relacionades entre sí, potser una base de dades és millor opció.
La serialització és el procés de salvar un objecte a un medi d'emmagatzematge (com pot ser un fitxer, o un buffer de memòria) amb la finalitat de transmetre'l a través d'una connexió en xarxa com una sèrie de bytes o en un format humanament més llegible, com per exemple XML. La sèrie de bytes o el format es poden utilitzar per recrear un objecte que és idèntic en el seu estat intern a l'objecte original. Aquest tipus de serialització s'utilitza principalment:
El format de les dades pot ser propietari o obert. Si treballem a un sol domini, podem utilitzar qualsevol dels dos formats. Alguns formats oberts són RDF, XML, Atom (dialecte de XML), JSON o YAML.
Els formats propietaris són molt nombrosos. Alguns estan documentats, i altres no. Alguns han variat el seu estatus al llarg del temps: DOC (Office) era tancat i no documentat, però Microsoft el va obrir al 2006.
Un arxiu de text pot codificar-se de diferents formes. La més habitual és el conjunt ASCII. Però això limita el nombre de caràcters a 128. El format més estès que soporta la majoria de caràcters d'escriptura és UTF-8.
El format de fi de línia és diferent per cada sistema operatiu: Windows (CR + LF), Unix/Linux (LF) i macOS (CR).
El format binari és una altra forma de dir als fitxers no de text. La seva estructura sol ser propietària, amb seqüencies de bytes. Solen tenir una capçalera que permet identificar-los, així com metadades per indicar les característiques del seu contingut. Exemples: arxius de vídeo i àudio, executables.
Un llenguatge de marques és un sistema per marcar o etiquetar un document de text per tal d'explicar la seva estructura lògica.
Característiques:
Tipus de llenguatges de marques:
HTML5 ha fet un intent de ser més descriptiu (semàntic) traient la part de presentació a CSS.
El llenguatge de marcatge extensible (XML) és un llenguatge de marques que defineix un conjunt de normes per a la codificació de documents en un format que sigui llegible per humans i que es pugui llegir amb màquina.
Com podem fer que una màquina o aplicació escrigui dades i altra les pugui llegir?
For the first question:
ID
, IDREF
, or ENTITY
, use an attribute.Principles to follow:
XML namespaces provide a way to distinguish duplicate element and attribute names. Duplicates element and attribute names can occur when an XML document contains elements and attributes from two or more different XML schemas (or DTDs). To quote Wikipedia: "In general, a namespace is an abstract container providing context for the items ... it holds and allows disambiguation of items having the same name."
An XML namespace is identified by an unique name (called a URI, not a URL, even though it can look like a URL). An URI is any string, although most people choose a URL-based URI because URLs are an easy way to hope for uniqueness. Although there's nothing preventing someone else from using the namespace http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul
, it's fairly unlikely anyone would choose that accidentally. Even if they did accidentally choose it, they may not define the same elements as XUL anyway (e.g., <textbox/>
) in their schema/DTD.
Any element type or attribute name in an XML namespace can be uniquely identified by its XML namespace and its "local name". Together, these two items define a qualified name, or QName.
For example, <xul:textbox/>
uses a namespace named "xul" and a local name "textbox". This distinguishes it from, for example, <foobar:textbox/>
which might occur in the same document. The xul and foobar namespaces must be defined at the top of the XML document in which they are used, like so:
<foobar:some-element
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:foobar="the-foobar-namespace">
<xul:textbox id="foo" value="bar"/>
<foobar:textbox favorite-food="pancakes"/>
</foobar:some-element>
Notice I've mixed two <textboxes/>
in the same document. The only way to distinguish that they have different meanings is with namespaces.
There's only one other thing to know: "default namespace". Every XML element has a "default namespace", and this is used with XUL elements all the time. In XUL documents, you'll usually see this:
<window
id="foo"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
...
...
</window>
and in XHTML documents, you'll see this:
<html xmlns="http://www.w3.org/1999/xhtml">
...
...
</html>
There is a very subtle difference here than before. Before I wrote xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
but here the :xul piece is omitted. This signifies to the XML parser that http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul
is the default namespace for the element and its descendant elements (unless further overridden by a default namespace on a descendant element), and that any element without a namespace (i.e., no prefix and colon) belongs to the default namespace. That's why we can write the shorthand <textbox/>
instead of <xul:textbox/>
in XUL (although the latter is just as correct when not using http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul
as the default namespace) -- the XUL namespace is defined as the default on the topmost element. In other words, a default namespace permits a kind of short-hand to be used for all descendants of an element.