comp.lang.ada
 help / color / mirror / Atom feed
* Generating an XML DOM tree from scratch withXML/Ada 2013
@ 2014-03-20 17:00 Marc C
  2014-03-21 16:12 ` J Kimball
  0 siblings, 1 reply; 3+ messages in thread
From: Marc C @ 2014-03-20 17:00 UTC (permalink / raw)


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


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

* Re: Generating an XML DOM tree from scratch withXML/Ada 2013
  2014-03-20 17:00 Generating an XML DOM tree from scratch withXML/Ada 2013 Marc C
@ 2014-03-21 16:12 ` J Kimball
  2014-03-24 20:18   ` Marc C
  0 siblings, 1 reply; 3+ messages in thread
From: J Kimball @ 2014-03-21 16:12 UTC (permalink / raw)


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;

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

* Re: Generating an XML DOM tree from scratch withXML/Ada 2013
  2014-03-21 16:12 ` J Kimball
@ 2014-03-24 20:18   ` Marc C
  0 siblings, 0 replies; 3+ messages in thread
From: Marc C @ 2014-03-24 20:18 UTC (permalink / raw)


On Friday, March 21, 2014 11:12:11 AM UTC-5, J Kimball wrote:
> 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.

I appreciate the effort on your part, but it didn't address my problem of programmatically creating an XML document from scratch.

Fortunately I figured out how to go about it, by sorta abusing some of the XML/Ada services (in the following code fragments the withs and uses are omitted, and suitable variable declarations are presumed):

You have to have a DOM_Implementation variable, such as DOM_Impl. Don't worry about getting it or initializing it. Merely declaring it is sufficient.

  DOM_Impl : DOM.Core.DOM_Implementation;

Create a document:

   Doc := Create_Document(DOM_Impl);

Create the root element with its namespace reference:

   Root_Element := Create_Element_NS(Doc, "myNS",
                                          "myNS:rootElement");

Abuse the set attribute service:

   Elements.Set_Attribute_NS
       (Root_Element,
        "xmlns",
        "xmlns:myNS",
        "http://path-to-namespace/more/and/more");

And from this point on you can build the document using Create_Element_NS, Append_Child, etc. to your heart's content.

Marc


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

end of thread, other threads:[~2014-03-24 20:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-20 17:00 Generating an XML DOM tree from scratch withXML/Ada 2013 Marc C
2014-03-21 16:12 ` J Kimball
2014-03-24 20:18   ` Marc C

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