martedì 19 marzo 2013


HABEMUS MBC!


 HOW TO VALIDATE XML FROM XSD WITH NOTEPAD++ AND XMLTOOLS PLUGIN
-
COME VALIDARE UN XML DA UN XSD USANDO NOTEPAD++ E IL PLUGIN XMLTOOLS


Hi guys,

this evening I wanna only suggest you a quick trick for validating an XML from his XSD.

How you all know, XML is a markup language (it means eXtensible Markup Language), that means you can create your own structure of document ONLY defining the properties of the elements inside, and every "translator" can receive informations from it.
A raw method for translating XML docs is creating an XML parser by yourself: if you know the structure of the document, you can get all the infos you need.

But what if, for example, you expect an integer and you get a string instead? You get an "unbelievable" parsing error in every programming language known and unknown (at least as far as I know).

So what you need? A structured document containing the rules about every element of your xml. How it's long, which type it is, the chars admitted too!!!
In one word, you need an XSD document (Xml Schema Definition). You can create your own, by your hand or using a thousand tools, but in most cases, you'll create an XML and you'll need to validate it according to an existing XSD received by a third party, a customer or a web service.

At this point you have two choices:

  1. Create a parser that receive an XML and ITS XSD, and then check all XML elements according the rules included in XSD (that's a very good programming exercise about XML-XSD, but it needs time and it may not be helpful if you hurry);
  2. Find an existing tool or library that does it;
Because I'm always in a hurry, I've chosen the second.Googling for the web, I found that my favourite editor, Notepad++, has what I was looking for: a golden plugin that validate an XML from an XSD. His name is, with a lot of imagination, XML Tools.

Let's see how to get it.

If you have not, download Notepad++ from here. It's free. 
And now that the force is strong with you, install and open it.
Go in "Plugins -> Plugin Manager -> Show Plugin Manager", then scroll through the list and check "XML Tools". Click on install and wait a few seconds.
1.1 How to install XML Tools
Now you got XML Tools.
But how to use it? It's very quick and simple.
With this plugin you can do a lot of useful thing, like formatting your text using the "Pretty" functions, or "Enable auto-validation" at every saving...
However, first open your XSD document. Then go "Plugins -> XML Tools -> Validate now".

Doing that you can check that your XSD is well-formed. For example, a common error is "XML Parsing error at line XX: Extra content at the end of the document".
Then you go to the end of the document, and nothing: it's all ok... What the hell's happening?
Simple: you get THIS error:

1.2 XML Parsing error at line XX: Extra content at the end of the document
You see them? You have HORRIBLY KILLED two space blancks!
Correct them, and this error will disappear... Thank me: I've saved you an hour during a full emergency "deadline on arrival"!

Ok, now go on your XML. Click on "Validate now". If your XML can't reach the associated XSD, a prompt appear, and you must select the directory of the XSD. After that validate again: another dialog appears, and
  1. can tell you that the validation is successful;
  2. that you have an error, indicating the relative ELEMENT, the VALUE and the XSD RULE, in order to allow the immediate identification of the error (they could also indicate the line number, but that's okay...)
1.3 Showing the error 


Note that the dialog box may also not appear: but it happens ONLY when XML is validated.o
That's all. Bye!



HABEMUS MBC!


Ciao ragazzi,

stasera voglio solo suggerirvi un trucchetto veloce per validare un XML partendo dal suo XSD.

Come tutti sapete, XML è un linguaggio di markup o marcatore (significa eXtensible Markup Language), e ciò significa che potete creare la vostra struttura di documento solo definendo le proprietà degli elementi all'interno, ed ogni "traduttore" può riceverne le informazioni.
Un metodo grezzo per tradurre documenti XML è creare voi stessi un parser XML: se conoscete la struttura del documento, potete ottenere tutte le informazioni di cui avete bisogno.

Ma che accade se, per esempio, vi aspettate un intero e, invece, ottenete una stringa? Ottenete un "incredibile" errore di parsing in qualsiasi linguaggio di programmazione conosciuto e sconosciuto (almeno per quanto ne so io).


Quindi cosa vi serve? Un documento strutturato contenente le regola su ogni elemento del vostro xml. Quanto è lungo, di che tipo è, anche i caratteri ammessi!!!

In una parola, vi serve un documento XSD (Xml Schema Definition). Potete creare il vostro, con le vostre mani o usando migliaia di strumenti, ma nella maggior parte dei casi, creerete un XML e avrete bisogno di convalidarlo secondo un XSD già esistente ricevuto da una terza parte, da un cliente o da un web service.

A questo punto avete due scelte:

  1. Creare un parser che riceve un XML ed il suo XSD, e quindi controllare tutto l'XML secondo le regole contenute nell'XSD (è un esercizio di programmazione veramente buono sull'XML-XSD, ma richiede tempo e può non essere utile se avete fretta);
  2. Trovare uno strumento o una libreria esistente che lo faccia;
Poiché ho SEMPRE fretta, ho optato per la seconda.

Googlando per il web, ho trovato che il mio editor preferito, Notepad++, ha ciò che cercavo: un plugin "dorato" che valida un XML da un XSD. Il suo nome è, con molta fantasia, XML Tools.

Vediamo come ottenerlo.

Se non lo avete, scaricate Notepad++ da qui. E' GRATIS. 

E ora che la forza è potente in voi, installatelo e apritelo.
Andate in "Plugins -> Plugin Manager -> Show Plugin Manager", quindi scorrete lungo la lista e selezionate  "XML Tools". Cliccate su "Install" e attendete alcuni secondi.

1.1 Come installare XML Tools
Ora avete XML Tools.
Ma come usarlo? E' molto veloce e semplice.
Con questo plugin potete fare un sacco di cose utili, come formattare il vostro testo usando le funzioni "Pretty", o abilitare la funzione di auto validazione "Enable auto-validation" ad ogni salvataggio...
Comunque, per prima cosa aprite il vostro documento XSD. Quindi andate su "Plugins -> XML Tools -> Validate now".

Così facendo potete controllare che il vostro XSD sia ben costruito. Ad esempio, un errore comune è  "XML Parsing error at line XX: Extra content at the end of the document".
Al ché voi andate alla fine del documento e... NULLA: è tutto ok... Che diavolo è successo?
Semplice: avete ottenuto questo errore

1.2 XML Parsing error at line XX: Extra content at the end of the document
Lo vedete? Avete ORRIBILMENTE TRUCIDATO due spazi!
Corregeteli, e l'errore sparirà... Ringraziatemi: vi ho fatto guadagnare un'ora durante una piena emergenza "scadenza in arrivo"!

Ok, ora andate su XML. Cliccate su "Validate now". Se il vostro XML non riesce a raggiungere l'XSD associato, apparirà una finestra di dialogo, e dovrete selezionare la directory del vostro XSD. Fatto ciò validate di nuovo: apparirà un'altra finestra di dialogo, e 
  1. potrà dirvi che la validazione è avvenuta con successo;
  2. che c'è un errore, indicando il relativo ELEMENTO, il VALORE e la REGOLA XSD, al fine di consentirvi l'identificazione immediata dell'errore (avrebbero anche potuto indicare il numero di riga, ma va bene lo stesso...)
1.3 Visualizzazione dell'errore


Si noti che la finestra di dialogo può anche non apparire: ma accade SOLO quando l'XML è validato.
Questo è tutto. Ciao!

Nessun commento:

Posta un commento