- What kind of elements can an element hold? - What kind of attributed can the element have? - What data type is allowed for them? In the general case, this is pretty hairy, but in our case it should be simple: - Element ordering is never important. - Elements are always either 0 or more, 1 or more, or exactly one.- There's nothing that requires me to do switches (no rules like <A> contains *either * <B> *or* <C>)
I've been able to get the structure from the DTD. Sample code below (comments appreciated). However, I'm stumped when I have to do this with either RelaxNG or Schema definitions. And if I understand correctly, I'm pretty limited in what I can do with DTDs in terms of Data Type constraints (can't say "non-negative integer, 4 digits") so I would much rather be using schemas...
Anyone that can point me in the right direction here? Enno. Some Example Code: extern "C" { #include <libxml/tree.h> #include <libxml/valid.h> } static void PrintElement(xmlElementContentPtr content, void *) { std::cout << " " << content->name << std::endl; } voidRunElements(xmlElementContentPtr content, void (*operation)(xmlElementContentPtr, void*), void * userdata)
{ switch (content->type) { case XML_ELEMENT_CONTENT_ELEMENT: operation(content, userdata); break; case XML_ELEMENT_CONTENT_SEQ: RunElements(content->c1, operation, userdata); RunElements(content->c2, operation, userdata); break; case XML_ELEMENT_CONTENT_OR: RunElements(content->c1, operation, userdata); RunElements(content->c2, operation, userdata); break; default: break; } } void ShowStructure(xmlDocPtr doc, const xmlChar * elemName) { xmlDtdPtr dtd = xmlParseDTD(NULL, BAD_CAST "editor.dtd"); xmlElementPtr element = xmlGetDtdElementDesc(dtd, elemName); std::cout << "attributes of " << element->name <<":\n"; xmlAttributePtr attrib = element->attributes; while (attrib!=NULL) { std::cout << " " << attrib->name << std::endl; attrib = attrib->nexth; } std::cout << "elements of " << element->name <<":\n"; RunElements(element->content, PrintElement, NULL); xmlFreeDtd(dtd); } int main(int argc, char** argv) { xmlDocPtr doc = xmlParseFile("conversations.xml"); ShowStructure(doc, BAD_CAST "location"); return 0; } -- If C++ has taught me one thing, it's this: Just because the system is consistent doesn't mean it's not the work of Satan. (Andrew Plotkin)
Attachment:
pgpnXvheaRyJh.pgp
Description: PGP signature