comp.lang.ada
 help / color / mirror / Atom feed
From: J Kimball <nntp@kmbll.com>
Subject: Re: XML/Ada schema validation and XML namespaces
Date: Wed, 10 Apr 2013 12:42:26 -0500
Date: 2013-04-10T12:42:26-05:00	[thread overview]
Message-ID: <kk48a3$79q$1@hugin.sparre-andersen.dk> (raw)
In-Reply-To: <lytxngexm7.fsf@pushface.org>

On 04/09/2013 06:45 AM, Simon Wright wrote:
> J Kimball <nntp@kmbll.com> writes:
> 
>> I'm having the derndest time figuring out how to validate XML using
>> namespaces against a schema with XML/Ada. I seem to recall that doing
>> away with namespaces makes it work. After all the debug junk I don't
>> care about scrolls past, I see:
>>
>> Validation_Error: Attribute "xmlns:agent" invalid for type
>> {http://singo1.com/schemas/agent}agent-message
>> Exception name: SCHEMA.VALIDATORS.XML_VALIDATION_ERROR
>>
>> it seems like an issue with XML/Ada. Can you tell me if I'm missing a
>> feature to enable, something about XML that I'm missing or just wrong
>> code or an example that is using namespaces that works?
> 
> I think you may have been using the AdaCore documentation at [1]?
> 
> If so, I think the problem is that the examples in 6.4, 6.5 don't
> actually set the grammar to be used by the reader; in 6.3 there's a
> declare block which shows you how to do it, eg
> 
>    with Sax.Readers;           use Sax.Readers;
>    with Schema.Readers;        use Schema.Readers;
>    with Schema.Schema_Readers; use Schema.Schema_Readers;
>    with Schema.Validators;     use Schema.Validators;
>    with Input_Sources.File;    use Input_Sources.File;
> 
>    with Ada.Text_IO; use Ada.Text_IO;
> 
>    procedure SchemaExample is
>       Grammar : XML_Grammar;
>       Schema  : Schema_Reader;
>       Input : File_Input;
>       My_Reader : Validating_Reader;
>    begin
>       Open ("agent.xsd", Input);
>       Parse (Schema, Input);
>       Close (Input);
> 
>       Grammar := Get_Grammar (Schema);
> 
>       Set_Public_Id (Input, "Agent file");
>       Open ("agent.xml", Input);
> 
>       Set_Grammar (My_Reader, Grammar);
>       Set_Feature (My_Reader, Schema_Validation_Feature, True);
>       Parse (My_Reader, Input);
> 
>       Close (Input);
> 
>       Put_Line ("OK.");
>    end SchemaExample;
> 
> which outputs "OK.".
> 
> I'm about to report this problem to AdaCore.
> 
> [1] http://docs.adacore.com/xmlada-docs/schema.html
> 

I get the same result when using your example. But when I try to fetch
elements from the DOM tree (Source is below), I get exceptions. For this
demo I've dumped the files into strings so the barrier to testing is
lower. Looking at XML documentation, local name doesn't include a prefix
and the namespace URI is obvious.

I compile with:

% gnatmake -gnatf -v -s -g -p -P schemaexample -bargs -E

When I run it I receive this:

% ./schemaexample

Execution terminated by unhandled exception
Exception name: CONSTRAINT_ERROR
Message: schemaexample.adb:59 discriminant check failed
Call stack traceback locations:
0x403b9f 0x4035f7 0x7fd1773c8eab 0x402b5b

% addr2line --demangle=gnat --functions --exe=schemaexample 0x403b9f
0x4035f7 0x7fb3dfc66eab 0x402b5b
schemaexample
/home/jkimball/websockets/schemaexample.adb:59
main
/home/jkimball/websockets/b~schemaexample.adb:508
??
??:0
<_start>
??:0

I'm working on a debug version of XML/Ada so the second half of these
addresses mean something to addr2line.


with Sax.Readers;           use Sax.Readers;
with Schema.Readers;        use Schema.Readers;
with Schema.Schema_Readers; use Schema.Schema_Readers;
with Schema.Validators;     use Schema.Validators;
with Input_Sources.Strings;    use Input_Sources.Strings;
with DOM.Core; use DOM.Core;
with DOM.Core.Elements;
with DOM.Core.Nodes;
with Ada.Text_IO; use Ada.Text_IO;
with Schema.DOM_Readers; use Schema.DOM_Readers;
with Unicode.CES.Utf8;

procedure SchemaExample is
   XSD : constant String := "<?xml version=""1.0"" encoding=""UTF-8""?>"
      & "<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified""
targetNamespace=""http://singo1.com/schemas/agent""
xmlns:agent=""http://singo1.com/schemas/agent"">"
      & "  <xs:element name=""response"" type=""agent:agent-message""/>"
      & "  <xs:complexType name=""agent-message"">"
      & "    <xs:sequence>"
      & "      <xs:element ref=""agent:message""/>"
      & "    </xs:sequence>"
      & "  </xs:complexType>"
      & "  <xs:element name=""message"" type=""xs:string""/>"
      & "</xs:schema>";

   XML : constant String := "<?xml version=""1.0""?>"
      & "<agent:response xmlns:agent=""http://singo1.com/schemas/agent"">"
      & "  <agent:message>Zerg</agent:message>"
      & "</agent:response>";

   Grammar   : XML_Grammar;
   S_Reader  : Schema_Reader;
   Input     : String_Input;
   My_Reader : Schema.DOM_Readers.Tree_Reader;

   Document : DOM.Core.Document;

   Messages : DOM.Core.Node_List;
   Length   : Natural;

   Agent_NS : constant String := "http://singo1.com/schemas/agent";
begin
   Input_Sources.Strings.Open (Str => XSD, Encoding =>
Unicode.CES.Utf8.Utf8_Encoding, Input => Input);
   Parse (S_Reader, Input);
   Close (Input);

   Grammar := Get_Grammar (S_Reader);

   Set_Public_Id (Input, "Agent file");
   Input_Sources.Strings.Open (Str => XML, Encoding =>
Unicode.CES.Utf8.Utf8_Encoding, Input => Input);

   Set_Grammar (My_Reader, Grammar);
   Set_Feature (My_Reader, Schema_Validation_Feature, True);
   Parse (My_Reader, Input);

   Close (Input);

   Document := Schema.DOM_Readers.Get_Tree (My_Reader);

   Messages := DOM.Core.Elements.Get_Elements_By_Tag_Name_NS (Elem =>
Document, Namespace_URI => Agent_NS, Local_Name => "message");
   Length := DOM.Core.Nodes.Length (Messages);

   Schema.DOM_Readers.Free (My_Reader);
   DOM.Core.Free (Messages);

   Put_Line ("OK.");
end SchemaExample;




  reply	other threads:[~2013-04-10 17:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-08 19:58 XML/Ada schema validation and XML namespaces J Kimball
2013-04-08 20:00 ` J Kimball
2013-04-09 11:45 ` Simon Wright
2013-04-10 17:42   ` J Kimball [this message]
2013-04-10 19:56     ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox