GSA said:
Hi,
I need to create structures from DTD. But it donot know what sort of
mapping I can do to the following DTD types( whether to structures/
unions/enums).
<!ELEMENT note (#PCDATA|to|from|header|message)*>
<!ELEMENT note (to,from,header,(message|body))>
<!ELEMENT note (message?)>
<!ELEMENT note (message+)>
(taken from
http://www.w3schools.com/dtd/dtd_elements.asp)
I donot know what C construct to use for these different notations. I
browsed the net but not much specific info. Please help.
use DOM-like nodes for XML.
use DOM-like nodes for DTD's.
just it uses modified parsing/printing logic.
as for evaluating DTD's, this is another matter.
for example (not quite DOM, but from my compiler, which uses XML
internally):
typedef struct BCCX_Node_s BCCX_Node;
typedef struct BCCX_Attr_s BCCX_Attr;
struct BCCX_Node_s {
BCCX_Node *next; //next node in current list (same parent)
BCCX_Node *prev; //prior node in current list (same parent)
BCCX_Node *up; //parent node
BCCX_Node *down; //first child node
BCCX_Node *down_end; //last child node (insert optimization)
BCCX_Attr *attr; //attributes
char *ns; //namespace (optional)
char *tag; //tagname (excludes text for normal nodes)
char *text; //textual data (excludes ns/tag/attr for
normal nodes)
BCCX_Node *hnext; //hash next (lookup optimization, exact use depends on
context)
int type; //node type
};
struct BCCX_Attr_s {
BCCX_Attr *next; //next attribute (same node)
char *ns; //namespace
char *var; //attribute name
char *val; //attribute value
};
comments were expanded here.
side notes:
I usually intern strings so that the '==' operator can be used to check for
string equivalence (faster than strcmp for longer lookups, although a lookup
will require interning the tags/values to be looked-up which is not free,
and to do so cheaply would require keeping track of any commonly-used
strings in variables);
the 'Document' context is usually implicit, and not usually referenced by
its contained nodes;
I don't optimize for integer/real attributes, which is an issue as
integer/real attributes are common, but there is no "good" way to handle
them (apart from adding another field to 'Attr' to hold them, so I mostly
ignore issue);
....
same nodes can be used for both purposes, despite DTD's having a different
syntax from other XML (they can be mapped).
not sure what proper DOM does, not looked into this...
or such...