comp.lang.ada
 help / color / mirror / Atom feed
* XML/Ada schema validation and XML namespaces
@ 2013-04-08 19:58 J Kimball
  2013-04-08 20:00 ` J Kimball
  2013-04-09 11:45 ` Simon Wright
  0 siblings, 2 replies; 5+ messages in thread
From: J Kimball @ 2013-04-08 19:58 UTC (permalink / raw)


Hello

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?

Some XML (agent.xml):

<?xml version="1.0"?>

<agent:response xmlns:agent="http://example.com/schemas/agent">
   <agent:message>Zerg</agent:message>
</agent:response>

A schema (agent.xsd):

<?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://example.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>

For those who are so inclined, I generated the XML schema from a RelaxNG
schema (agent.rng) with trang:

<?xml version="1.0"?>

<rng:grammar
   xmlns:rng="http://relaxng.org/ns/structure/1.0"
   xmlns:a="http://relaxng.org/ns/annotation/1.0"
   xmlns:agent="http://example.com/schemas/agent"
   datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

   <rng:start>
      <rng:ref name="agent-response" />
   </rng:start>

   <rng:define name="agent-response">
      <a:documentation>A response from a client.</a:documentation>

      <rng:element name="agent:response">
         <rng:ref name="agent-message" />
      </rng:element>
   </rng:define>

   <rng:define name="agent-message">
      <a:documentation>A simple message.</a:documentation>

      <rng:element name="agent:message"><rng:data type="string"
/></rng:element>
   </rng:define>

</rng:grammar>

% trang agent.rng agent.xsd

agent.xml validates successfully against both schemas using jing and
xmllint:

% jing agent.rng agent.xml
% xmllint --relaxng agent.rng agent.xml
% xmllint --schema agent.xsd agent.xml




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: XML/Ada schema validation and XML namespaces
  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
  1 sibling, 0 replies; 5+ messages in thread
From: J Kimball @ 2013-04-08 20:00 UTC (permalink / raw)


There is a minor error in the XML schema. The correct schema is:

A schema (agent.xsd):

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://example.com/schemas/agent"
xmlns:agent="http://example.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>

On 04/08/2013 02:58 PM, J Kimball wrote:
> Hello
> 
> 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?
> 
> Some XML (agent.xml):
> 
> <?xml version="1.0"?>
> 
> <agent:response xmlns:agent="http://example.com/schemas/agent">
>    <agent:message>Zerg</agent:message>
> </agent:response>
> 
> A schema (agent.xsd):
> 
> <?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://example.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>
> 
> For those who are so inclined, I generated the XML schema from a RelaxNG
> schema (agent.rng) with trang:
> 
> <?xml version="1.0"?>
> 
> <rng:grammar
>    xmlns:rng="http://relaxng.org/ns/structure/1.0"
>    xmlns:a="http://relaxng.org/ns/annotation/1.0"
>    xmlns:agent="http://example.com/schemas/agent"
>    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
> 
>    <rng:start>
>       <rng:ref name="agent-response" />
>    </rng:start>
> 
>    <rng:define name="agent-response">
>       <a:documentation>A response from a client.</a:documentation>
> 
>       <rng:element name="agent:response">
>          <rng:ref name="agent-message" />
>       </rng:element>
>    </rng:define>
> 
>    <rng:define name="agent-message">
>       <a:documentation>A simple message.</a:documentation>
> 
>       <rng:element name="agent:message"><rng:data type="string"
> /></rng:element>
>    </rng:define>
> 
> </rng:grammar>
> 
> % trang agent.rng agent.xsd
> 
> agent.xml validates successfully against both schemas using jing and
> xmllint:
> 
> % jing agent.rng agent.xml
> % xmllint --relaxng agent.rng agent.xml
> % xmllint --schema agent.xsd agent.xml
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: XML/Ada schema validation and XML namespaces
  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
  1 sibling, 1 reply; 5+ messages in thread
From: Simon Wright @ 2013-04-09 11:45 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: XML/Ada schema validation and XML namespaces
  2013-04-09 11:45 ` Simon Wright
@ 2013-04-10 17:42   ` J Kimball
  2013-04-10 19:56     ` Simon Wright
  0 siblings, 1 reply; 5+ messages in thread
From: J Kimball @ 2013-04-10 17:42 UTC (permalink / raw)


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;




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: XML/Ada schema validation and XML namespaces
  2013-04-10 17:42   ` J Kimball
@ 2013-04-10 19:56     ` Simon Wright
  0 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2013-04-10 19:56 UTC (permalink / raw)


J Kimball <nntp@kmbll.com> writes:

>    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");

Well, in Dom.Core,
   subtype Document is Node (Document_Node);
but Get_Elements_By_Tag_Name_NS'e Elem parameter is an Element
   subtype Element is Node (Element_Node);

I think you need the first child of the document (I think there's only
one): Elem => DOM.Core.Nodes.First_Child (Document).



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-10 19:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2013-04-10 19:56     ` Simon Wright

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