From: J Kimball <spam@example.com>
Subject: Re: Generating an XML DOM tree from scratch withXML/Ada 2013
Date: Fri, 21 Mar 2014 11:12:11 -0500
Date: 2014-03-21T11:12:11-05:00 [thread overview]
Message-ID: <lghocs$ohn$1@loke.gir.dk> (raw)
In-Reply-To: <1f79df5e-7fbd-4f94-b2c7-8afa754b31d5@googlegroups.com>
On 03/20/2014 12:00 PM, Marc C wrote:
> After writing up some pathfinding code to simply create a DOM document tree from scratch, add a node, and write it out, I was perplexed that the namespace data I was supplying was not showing up in the document.
>
> Imagine my surprise when looking at the Dom.Core.Create_Document() implementation and seeing that the Namespace_URI (and Qualified_Name) parameters are simply ignored. The comment in the spec simply states "Note that Namespace_URI can be the empty string if you do not want to use namespaces."
>
> Well, I *do* want (and need) to use namespaces.
>
> The implementation of various subprograms reference namespaces and qualified names, and that seems to somehow tie into the SAX Symbol_Table that can be provided to Create_Document. But it ain't clear what one would have to do in that regard, not to mention I'm not sure I'm even reading the code right about all that.
>
> I've got XML_EZ_Out for outputting XML, but in this instance I preferred to build the DOM tree and manipulate it, then write the whole thing out when I was done.
>
> Anyone used the GNAT GPL 2013 XML/Ada packages for creating DOM trees with namespaces?
>
> Thanks.
>
> Marc A. Criley
>
I have done this. The organization and documentation of XML is perplexing and I've also spent hours banging my head against a wall trying to use namespaces and validation.
Here's a result of my labor. I hope you can make some sense of it. Unfortunately, I couldn't find the input files that i was using to test.
There are two command line arguments. I think the first is the xml file and the second is a valid XSD schema.
Good luck.
--
with Ada.Command_Line;
with Ada.Directories;
with Ada.Direct_IO;
with Ada.Exceptions;
with Ada.Text_IO;
with DOM.Core.Elements;
with DOM.Core.Nodes;
with Input_Sources.File;
with Input_Sources.Strings;
with SAX.Readers;
with Schema.DOM_Readers;
with Schema.Schema_Readers;
with Schema.Validators;
with Unicode.CES.Utf8;
procedure Validate_Message is
Grammar : Schema.Validators.XML_Grammar;
Agent_NS : String := "http://example.com/schemas/agent";
function Get_Message (XML : String) return String is
Input : Input_Sources.Strings.String_Input;
Reader : Schema.DOM_Readers.Tree_Reader;
Document : DOM.Core.Document;
Elements : DOM.Core.Node_List;
begin
Input_Sources.Strings.Open (Str => XML, Encoding => Unicode.CES.Utf8.Utf8_Encoding, Input => Input);
Schema.DOM_Readers.Set_Grammar (Reader, Grammar);
Schema.DOM_Readers.Set_Feature (Reader, SAX.Readers.Namespace_Prefixes_Feature, True);
Schema.DOM_Readers.Set_Feature (Reader, SAX.Readers.Namespace_Feature, True);
-- Schema.DOM_Readers.Set_Feature (Reader, SAX.Readers.Validation_Feature, True);
Schema.DOM_Readers.Parse (Reader, Input);
Input_Sources.Strings.Close (Input);
Document := Schema.DOM_Readers.Get_Tree (Reader);
Schema.DOM_Readers.Free (Reader);
Elements := DOM.Core.Elements.Get_Elements_By_Tag_Name_NS (Elem => Document, Namespace_URI => Agent_NS, Local_Name => "message");
return DOM.Core.Nodes.Node_Value (DOM.Core.Nodes.Item (Elements, 0) );
exception
when E : others =>
Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E) );
raise;
end Get_Message;
function Init_Grammar return Schema.Validators.XML_Grammar is
Reader : Schema.Schema_Readers.Schema_Reader;
Input : Input_Sources.File.File_Input;
begin
Input_Sources.File.Open (Ada.Command_Line.Argument (2), Input);
Schema.Schema_Readers.Parse (Reader, Input);
Input_Sources.File.Close (Input);
return Schema.Schema_Readers.Get_Grammar (Reader);
end Init_Grammar;
File_Size : Ada.Directories.File_Size := Ada.Directories.Size (Ada.Command_Line.Argument (1) );
subtype File_Input is String (1..Integer (File_Size) );
package File_IO is new Ada.Direct_IO (Element_Type => File_Input);
File : File_IO.File_Type;
Input : File_Input;
begin
Schema.Set_Debug_Output (True);
-- Schema.Dump_Internal_XSD := True;
File_IO.Open (File => File, Mode => File_IO.In_File, Name => Ada.Command_Line.Argument (1) );
File_IO.Read (File => File, Item => Input);
File_IO.Close (File => File);
Grammar := Init_Grammar;
Ada.Text_IO.Put_Line (Get_Message (Input) );
end Validate_Message;
next prev parent reply other threads:[~2014-03-21 16:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-20 17:00 Generating an XML DOM tree from scratch withXML/Ada 2013 Marc C
2014-03-21 16:12 ` J Kimball [this message]
2014-03-24 20:18 ` Marc C
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox