📄 schematron.c
字号:
return; } else { ruleptr = xmlSchematronAddRule(ctxt, ctxt->schema, pattern, rule, context, NULL); if (ruleptr == NULL) { xmlFree(context); return; } } cur = rule->children; NEXT_SCHEMATRON(cur); while (cur != NULL) { if (IS_SCHEMATRON(cur, "assert")) { nbChecks++; test = xmlGetNoNsProp(cur, BAD_CAST "test"); if (test == NULL) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "assert has no test attribute", NULL, NULL); } else if (test[0] == 0) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "assert has an empty test attribute", NULL, NULL); xmlFree(test); } else { /* TODO will need dynamic processing instead */ report = xmlNodeGetContent(cur); testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_ASSERT, ruleptr, cur, test, report); if (testptr == NULL) xmlFree(test); } } else if (IS_SCHEMATRON(cur, "report")) { nbChecks++; test = xmlGetNoNsProp(cur, BAD_CAST "test"); if (test == NULL) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "assert has no test attribute", NULL, NULL); } else if (test[0] == 0) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "assert has an empty test attribute", NULL, NULL); xmlFree(test); } else { /* TODO will need dynamic processing instead */ report = xmlNodeGetContent(cur); testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_REPORT, ruleptr, cur, test, report); if (testptr == NULL) xmlFree(test); } } else { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "Expecting an assert or a report element instead of %s", cur->name, NULL); } cur = cur->next; NEXT_SCHEMATRON(cur); } if (nbChecks == 0) { xmlSchematronPErr(ctxt, rule, XML_SCHEMAP_NOROOT, "rule has no assert nor report element", NULL, NULL); }}/** * xmlSchematronParsePattern: * @ctxt: a schema validation context * @pat: the pattern node * * parse a pattern element */static voidxmlSchematronParsePattern(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr pat){ xmlNodePtr cur; xmlSchematronPatternPtr pattern; int nbRules = 0; xmlChar *id; if ((ctxt == NULL) || (pat == NULL)) return; id = xmlGetNoNsProp(pat, BAD_CAST "id"); if (id == NULL) { id = xmlGetNoNsProp(pat, BAD_CAST "name"); } pattern = xmlSchematronAddPattern(ctxt, ctxt->schema, pat, id); if (pattern == NULL) { if (id != NULL) xmlFree(id); return; } cur = pat->children; NEXT_SCHEMATRON(cur); while (cur != NULL) { if (IS_SCHEMATRON(cur, "rule")) { xmlSchematronParseRule(ctxt, pattern, cur); nbRules++; } else { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "Expecting a rule element instead of %s", cur->name, NULL); } cur = cur->next; NEXT_SCHEMATRON(cur); } if (nbRules == 0) { xmlSchematronPErr(ctxt, pat, XML_SCHEMAP_NOROOT, "Pattern has no rule element", NULL, NULL); }}#if 0/** * xmlSchematronLoadInclude: * @ctxt: a schema validation context * @cur: the include element * * Load the include document, Push the current pointer * * Returns the updated node pointer */static xmlNodePtrxmlSchematronLoadInclude(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr cur){ xmlNodePtr ret = NULL; xmlDocPtr doc = NULL; xmlChar *href = NULL; xmlChar *base = NULL; xmlChar *URI = NULL; if ((ctxt == NULL) || (cur == NULL)) return(NULL); href = xmlGetNoNsProp(cur, BAD_CAST "href"); if (href == NULL) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "Include has no href attribute", NULL, NULL); return(cur->next); } /* do the URI base composition, load and find the root */ base = xmlNodeGetBase(cur->doc, cur); URI = xmlBuildURI(href, base); doc = xmlReadFile((const char *) URI, NULL, SCHEMATRON_PARSE_OPTIONS); if (doc == NULL) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_FAILED_LOAD, "could not load include '%s'.\n", URI, NULL); goto done; } ret = xmlDocGetRootElement(doc); if (ret == NULL) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_FAILED_LOAD, "could not find root from include '%s'.\n", URI, NULL); goto done; } /* Success, push the include for rollback on exit */ xmlSchematronPushInclude(ctxt, doc, cur);done: if (ret == NULL) { if (doc != NULL) xmlFreeDoc(doc); } xmlFree(href); if (base != NULL) xmlFree(base); if (URI != NULL) xmlFree(URI); return(ret);}#endif/** * xmlSchematronParse: * @ctxt: a schema validation context * * parse a schema definition resource and build an internal * XML Shema struture which can be used to validate instances. * * Returns the internal XML Schematron structure built from the resource or * NULL in case of error */xmlSchematronPtrxmlSchematronParse(xmlSchematronParserCtxtPtr ctxt){ xmlSchematronPtr ret = NULL; xmlDocPtr doc; xmlNodePtr root, cur; int preserve = 0; if (ctxt == NULL) return (NULL); ctxt->nberrors = 0; /* * First step is to parse the input document into an DOM/Infoset */ if (ctxt->URL != NULL) { doc = xmlReadFile((const char *) ctxt->URL, NULL, SCHEMATRON_PARSE_OPTIONS); if (doc == NULL) { xmlSchematronPErr(ctxt, NULL, XML_SCHEMAP_FAILED_LOAD, "xmlSchematronParse: could not load '%s'.\n", ctxt->URL, NULL); return (NULL); } ctxt->preserve = 0; } else if (ctxt->buffer != NULL) { doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, SCHEMATRON_PARSE_OPTIONS); if (doc == NULL) { xmlSchematronPErr(ctxt, NULL, XML_SCHEMAP_FAILED_PARSE, "xmlSchematronParse: could not parse.\n", NULL, NULL); return (NULL); } doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1); ctxt->preserve = 0; } else if (ctxt->doc != NULL) { doc = ctxt->doc; preserve = 1; ctxt->preserve = 1; } else { xmlSchematronPErr(ctxt, NULL, XML_SCHEMAP_NOTHING_TO_PARSE, "xmlSchematronParse: could not parse.\n", NULL, NULL); return (NULL); } /* * Then extract the root and Schematron parse it */ root = xmlDocGetRootElement(doc); if (root == NULL) { xmlSchematronPErr(ctxt, (xmlNodePtr) doc, XML_SCHEMAP_NOROOT, "The schema has no document element.\n", NULL, NULL); if (!preserve) { xmlFreeDoc(doc); } return (NULL); } if (!IS_SCHEMATRON(root, "schema")) { xmlSchematronPErr(ctxt, root, XML_SCHEMAP_NOROOT, "The XML document '%s' is not a XML schematron document", ctxt->URL, NULL); goto exit; } ret = xmlSchematronNewSchematron(ctxt); if (ret == NULL) goto exit; ctxt->schema = ret; /* * scan the schema elements */ cur = root->children; NEXT_SCHEMATRON(cur); if (IS_SCHEMATRON(cur, "title")) { xmlChar *title = xmlNodeGetContent(cur); if (title != NULL) { ret->title = xmlDictLookup(ret->dict, title, -1); xmlFree(title); } cur = cur->next; NEXT_SCHEMATRON(cur); } while (IS_SCHEMATRON(cur, "ns")) { xmlChar *prefix = xmlGetNoNsProp(cur, BAD_CAST "prefix"); xmlChar *uri = xmlGetNoNsProp(cur, BAD_CAST "uri"); if ((uri == NULL) || (uri[0] == 0)) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "ns element has no uri", NULL, NULL); } if ((prefix == NULL) || (prefix[0] == 0)) { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "ns element has no prefix", NULL, NULL); } if ((prefix) && (uri)) { xmlXPathRegisterNs(ctxt->xctxt, prefix, uri); xmlSchematronAddNamespace(ctxt, prefix, uri); ret->nbNs++; } if (uri) xmlFree(uri); if (prefix) xmlFree(prefix); cur = cur->next; NEXT_SCHEMATRON(cur); } while (cur != NULL) { if (IS_SCHEMATRON(cur, "pattern")) { xmlSchematronParsePattern(ctxt, cur); ret->nbPattern++; } else { xmlSchematronPErr(ctxt, cur, XML_SCHEMAP_NOROOT, "Expecting a pattern element instead of %s", cur->name, NULL); } cur = cur->next; NEXT_SCHEMATRON(cur); } if (ret->nbPattern == 0) { xmlSchematronPErr(ctxt, root, XML_SCHEMAP_NOROOT, "The schematron document '%s' has no pattern", ctxt->URL, NULL); goto exit; } /* the original document must be kept for reporting */ ret->doc = doc; preserve = 1;exit: if (!preserve) { xmlFreeDoc(doc); } if (ret != NULL) { if (ctxt->nberrors != 0) { xmlSchematronFree(ret); ret = NULL; } else { ret->namespaces = ctxt->namespaces; ret->nbNamespaces = ctxt->nbNamespaces; ctxt->namespaces = NULL; } } return (ret);}/************************************************************************ * * * Schematrontron Reports handler * * * ************************************************************************/static xmlNodePtrxmlSchematronGetNode(xmlSchematronValidCtxtPtr ctxt, xmlNodePtr cur, const xmlChar *xpath) { xmlNodePtr node = NULL; xmlXPathObjectPtr ret; if ((ctxt == NULL) || (cur == NULL) || (xpath == NULL)) return(NULL); ctxt->xctxt->doc = cur->doc; ctxt->xctxt->node = cur; ret = xmlXPathEval(xpath, ctxt->xctxt); if (ret == NULL) return(NULL); if ((ret->type == XPATH_NODESET) && (ret->nodesetval != NULL) && (ret->nodesetval->nodeNr > 0)) node = ret->nodesetval->nodeTab[0]; xmlXPathFreeObject(ret); return(node);}/** * xmlSchematronReportOutput: * @ctxt: the validation context * @cur: the current node tested * @msg: the message output * * Output part of the report to whatever channel the user selected */static voidxmlSchematronReportOutput(xmlSchematronValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlNodePtr cur ATTRIBUTE_UNUSED, const char *msg) { /* TODO */ fprintf(stderr, "%s", msg);}/** * xmlSchematronFormatReport: * @ctxt: the validation context * @test: the test node * @cur: the current node tested * * Build the string being reported to the user. * * Returns a report string or NULL in case of error. The string needs * to be deallocated by teh caller */static xmlChar *xmlSchematronFormatReport(xmlSchematronValidCtxtPtr ctxt, xmlNodePtr test, xmlNodePtr cur) { xmlChar *ret = NULL; xmlNodePtr child, node; if ((test == NULL) || (cur == NULL)) return(ret); child = test->children; while (child != NULL) { if ((child->type == XML_TEXT_NODE) || (child->type == XML_CDATA_SECTION_NODE)) ret = xmlStrcat(ret, child->content); else if (IS_SCHEMATRON(child, "name")) { xmlChar *path; path = xmlGetNoNsProp(child, BAD_CAST "path"); node = cur; if (path != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -