[erlang-questions] xmerl scan file and validate using schema

Tobias Schlager Tobias.Schlager@REDACTED
Tue Jan 23 12:43:23 CET 2018


Hi Frans,

I've been using xmerl successfully a long time ago. The fetch_fun needs to provide the path to a local file. From what I can remember, I shipped all XSD files along with my application in its priv directory, to be able to validate without downloading the respective XSD every time. However, you could download them on-the-fly.

You could use something similar to the following as a start (code will crash if XSD cannot be found):

fetch_xsd(URL = "http://" ++ _, State = #xsd_state{}) ->
    FilePath = download(URL),
    {ok, {file, FilePath}, State};
fetch_xsd(Location, State = #xsd_state{fetch_path = Paths}) ->
    {ok, get_file(Location, Paths), State}.

get_file(File, [Dir | Rest]) ->
    Path = filename:join([Dir, File]),
    case filelib:is_file(Path) of
        true  ->
            {file, Path};
        false ->
            get_file(File, Rest)
    end.

My options looked like this: [{fetch_fun, fun fetch_xsd/2}, {fetch_path, [PrivDir]}]

Regards
Tobias

________________________________________
Von: erlang-questions-bounces@REDACTED [erlang-questions-bounces@REDACTED]" im Auftrag von "Frans Schneider [fchschneider@REDACTED]
Gesendet: Dienstag, 23. Januar 2018 11:39
An: Erlang Questions
Betreff: [erlang-questions] xmerl scan file and validate using schema

Dear list,

I would like to use validation while reading XML files using
xmerl_scan:file/2 or even better, using xmerl_xsd:validate/2 with a
previously processed schema. In both cases, I get the error
{fetch_fun_failed,{http,"http://www.w3.org/2001/xml.xsd"}}. However, the
documentation gives no details on what the fetch function should look
like and looking through the sources did not help much. I also made a
local copy of the imported xml.xsd file and tried several options
without luck.

My question is if somebody knows about an easy way to get this working?
The xsd's head is listed below.

NB: I know about Erlsom, which does work well but lacks line numbers in
the output which I need for reporting errors in the XML file.

Thanks,

Frans

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright OASIS Open 2010. All Rights Reserved. -->
<xs:schema xmlns:xacml="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"
        elementFormDefault="qualified" attributeFormDefault="unqualified">

        <xs:import namespace="http://www.w3.org/XML/1998/namespace"
                schemaLocation="http://www.w3.org/2001/xml.xsd"/>


_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list