xmerl_xsd(3)
π xmerl_xsd β the interface module for XML Schema validation π
okay bestie, gather round, because this is the one in the xmerl family you call when you need to check that an XML thing actually obeys its schema. This is the Erlang module definition page for xmerl_xsd, and it's lowkey iconic. π¦
β¨ NAME β¨
xmerl_xsd β Interface module for XML Schema validation. that's it, that's the vibe. π
πΈ DESCRIPTION πΈ
It handles the W3.org specifications of XML Schema second edition 28 october 2004. So yeah, it's doing the whole spec, and if you want an intro to XML Schema you should fr go study part 0 first. π
An XML structure gets validated by xmerl_xsd:validate/[2,3]. That's the moment. The main character moment. π
π DATA TYPES π
filename() :: string().β just a string, bestie. a name. of a file. wild concept ik.global_state() :: xsd_state().β the global state of the validator. β¨ It is represented by the#xsd_state{}record. no cap.option_list() :: [{xsdbase, filename()} | {atom(), term()}].β the options that let you customize the behaviour of the validation. π
Possible options are:
{tab2file,boolean()}β Enables saving of abstract structure on file for debugging purpose. yes, for debugging, bestie, not for flexing. π{xsdbase,filename()}β XSD Base directory. π{fetch_fun,FetchFun}β Call back function to fetch an external resource. it's giving "you go get it". π«Ά{fetch_path,PathList}βPathListis a list of directories to search when fetching files. If the file in question is not in thefetch_path, the URI will be used as a file name. so if it's not where you looked, we're just rawdogging the URI as a filename, that's the rule. π{state,State}β It is possible by this option to provide a state with process information from an earlier validation. reuse queen. β»οΈ
π₯ FUNCTIONS π₯
β file2state(FileName :: string()) -> {ok, State} | {error, Reason}
when State :: global_state(), Reason :: term().
Read the schema processing state from a file. π
Reads the schema state with all information of the processed schema from a file created with state2file/[1,2]. The format of this file is internal β so like, don't go poking at it, it's private. π The state can then be used validating an XML document. slay.
β format_error(Reasons) -> io_lib:chars()
when
Reasons ::
[Reason :: term()] | (Reason :: term()).
Format error descriptions to human readable strings. so your errors stop looking like cursed hieroglyphics and start looking like sentences. π
β process_schema(Schema :: string(), Options :: option_list()) -> {ok, State} | {error, Reasons}
when
State :: global_state(),
Reasons ::
[Reason :: term()] | (Reason :: term()).
Read an XML schema and check that it is valid. β
Reads the referenced XML schema and checks that it is valid. Returns the global_state/0 with schema info, or an error reason. The error reason may be a list of several errors or a single error encountered during the processing. so like, it might hand you one problem, or it might hand you the whole group chat. π
β process_schemas(Schemas, Options) -> {ok, State} | {error, Reasons}
when
Schemas :: [{NameSpace, Schema}, ...],
Options :: option_list(),
NameSpace :: term(),
Schema :: string(),
State :: global_state(),
Reasons ::
[Reason :: term()] | (Reason :: term()).
Read XML schemas β plural, babes, we're doing a set β and check that they are valid. β β
Reads the referenced XML schemas and controls they are valid. Returns the global_state/0 with schema info or an error reason. The error reason may be a list of several errors or a single error encountered during the processing. same energy as above but multiplied. π―
β process_validate(Schema, Element, Options) -> Result
when
Schema :: string(),
Element :: xmerl:element(),
Options :: option_list(),
Result ::
{ValidElement, global_state()} |
{error, Reasons},
ValidElement :: xmerl:element(),
Reasons ::
[Reason :: term()] |
(Reason :: term()).
Validate a parsed well-formed XML element towards an XML schema. π―
Validates in two steps. First it processes the schema, saves the type and structure info in an ETS table and then validates the element towards the schema. two steps, one icon. β¨
Usage example:
1>{E,_} = xmerl_scan:file("my_XML_document.xml").
2>{E2,_} = xmerl_xsd:process_validate("my_XML_Schema.xsd",E).
Observe thatE2may differ fromEif for instance there are default values defined inmy_XML_Schema.xsd. π³ yes bestie, your element can come back changed. that's the schema's default values doing their little makeover. π
β state2file(State :: global_state()) -> ok | {error, Reason}
when Reason :: term().
Equivalent to state2file(State, SchemaName).
SchemaName is the name of the schema in State. so it just grabs the name it already knows. convenient fr fr. π«Ά
β state2file(global_state(), FileName :: string()) -> ok | {error, Reason}
when Reason :: term().
Save the schema processing state to a file. πΎ
Saves the schema state with all information of the processed schema in a file. You can provide the file name for the saved state. FileName is saved with the .xss extension added. don't be surprised when the extension shows up, it was always the plan. π
β validate(Element, global_state(), Options) -> Result
when
Element :: xmerl:xmlElement(),
Options :: option_list(),
Result ::
{ValidElement, global_state()} |
{error, Reasons},
ValidElement :: xmerl:xmlElement(),
Reasons :: [ErrorReason] | ErrorReason,
ErrorReason :: term().
Validate a parsed well-formed XML element (Element). π
A call to validate/2 or validate/3 must provide a well formed parsed XML Element :: #xmlElement{} and a State, global_state/0, which holds necessary information from an already processed schema. Thus validate enables reuse of the schema information, and therefore if one shall validate several times towards the same schema it reduces time consumption. it's giving efficiency queen, it's giving caching, it's giving we-do-not-reprocess-in-this-house. π
The result, ValidElement, is the valid element that conforms to the post-schema-validation infoset. When the validator finds an error it tries to continue and reports a list of all errors found. In those cases an unexpected error is found it may cause a single error reason. so: a whole list of problems if it can keep going, or one sad lonely reason if it hit something truly unexpected. π
Usage example:
1>{E,_} = xmerl_scan:file("my_XML_document.xml").
2>{ok,S} = xmerl_xsd:process_schema("my_XML_Schema.xsd").
3>{E2,_} = xmerl_xsd:validate(E,S).
Observe thatE2may differ fromEif for instance there are default values defined inmy_XML_Schema.xsd. again! default values! they will absolutely reshape your element and that is correct behaviour, so don't come for me. πΈ
and that's the whole page, bestie π go validate something and feel powerful β¨
okay bye, this manual page came to you from Ericsson AB, describing xmerl version 2.2, dated with xmerl_xsd(3) as its page β so go forth and process those schemas. π¦π
π
π¦ where this page comes from
- nixos 26.05: erlang 29.1.2
- nixpkgs unstable: erlang 29.1.2
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms