1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-12-23 17:33:50 +03:00

allow to inherit attributes from the DTD directly in the tree, this is

* SAX.c testXPath.c valid.c xmllint.c include/libxml/valid.h:
  allow to inherit attributes from the DTD directly in the
  tree, this is needed for XPath and can be a useful feature.
  Inherited namespaces are always provided at the tree level now
* test/defattr* result/defattr* result/noent/defattr*: added a couple
  of tests for this feature (XSLT being the prime user).
Daniel
This commit is contained in:
Daniel Veillard 2001-08-07 01:10:10 +00:00
parent 50f3437111
commit 48da910097
12 changed files with 176 additions and 7 deletions

View File

@ -1,3 +1,12 @@
Tue Aug 7 03:05:58 CEST 2001 Daniel Veillard <daniel@veillard.com>
* SAX.c testXPath.c valid.c xmllint.c include/libxml/valid.h:
allow to inherit attributes from the DTD directly in the
tree, this is needed for XPath and can be a useful feature.
Inherited namespaces are always provided at the tree level now
* test/defattr* result/defattr* result/noent/defattr*: added a couple
of tests for this feature (XSLT being the prime user).
Fri Aug 3 14:02:20 CEST 2001 Daniel Veillard <daniel@veillard.com>
* DOCBparser.c Makefile.am nanohttp.c parser.c testHTML.c

73
SAX.c
View File

@ -21,6 +21,7 @@
#include <libxml/xmlIO.h>
#include <libxml/SAX.h>
#include <libxml/uri.h>
#include <libxml/valid.h>
#include <libxml/HTMLtree.h>
/* #define DEBUG_SAX */
@ -1032,6 +1033,78 @@ startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
}
}
/*
* Insert all the defaulted attributes from the DTD especially namespaces
*/
if ((!ctxt->html) &&
((ctxt->myDoc->intSubset != NULL) ||
(ctxt->myDoc->extSubset != NULL))) {
xmlElementPtr elemDecl = NULL;
if (prefix != NULL) {
if (ctxt->myDoc->intSubset != NULL)
elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset,
name, prefix);
if ((elemDecl == NULL) && (ctxt->myDoc->extSubset != NULL))
elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
name, prefix);
} else {
if (ctxt->myDoc->intSubset != NULL)
elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset,
name, prefix);
if ((elemDecl == NULL) && (ctxt->myDoc->extSubset != NULL))
elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
name, prefix);
}
if (elemDecl != NULL) {
xmlAttributePtr attr = elemDecl->attributes;
while (attr != NULL) {
if (attr->defaultValue != NULL) {
/*
* the element should be instanciated in the tree if:
* - this is a namespace prefix
* - the user required for completion in the tree
* like XSLT
*/
if (((attr->prefix != NULL) &&
(xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
((attr->prefix == NULL) &&
(xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
(ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
xmlChar buffer[100];
const xmlChar *fulln = attr->name;
if (attr->prefix != NULL) {
snprintf((char *) buffer, 99, "%s:%s",
attr->prefix, attr->name);
buffer[99] = 0;
fulln = buffer;
}
/*
* Check that the attribute is not declared in the
* serialization
*/
att = NULL;
if (atts != NULL) {
i = 0;
att = atts[i];
while (att != NULL) {
if (xmlStrEqual(att, fulln))
break;
i += 2;
att = atts[i];
}
}
if (att == NULL)
attribute(ctxt, fulln, attr->defaultValue);
}
}
attr = attr->nexth;
}
}
}
/*
* process all the attributes whose name start with "xml"
*/

View File

@ -230,8 +230,15 @@ int xmlIsMixedElement (xmlDocPtr doc,
xmlAttributePtr xmlGetDtdAttrDesc (xmlDtdPtr dtd,
const xmlChar *elem,
const xmlChar *name);
xmlAttributePtr xmlGetDtdQAttrDesc (xmlDtdPtr dtd,
const xmlChar *elem,
const xmlChar *name,
const xmlChar *prefix);
xmlNotationPtr xmlGetDtdNotationDesc (xmlDtdPtr dtd,
const xmlChar *name);
xmlElementPtr xmlGetDtdQElementDesc (xmlDtdPtr dtd,
const xmlChar *name,
const xmlChar *prefix);
xmlElementPtr xmlGetDtdElementDesc (xmlDtdPtr dtd,
const xmlChar *name);

6
result/defattr.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc xmlns CDATA #FIXED "http://www.example.com/">
]>
<doc xmlns="http://www.example.com/"/>

8
result/defattr2.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc defatt (0 | 1) "0">
<!ATTLIST doc xmlns:tst CDATA #FIXED "http://example.org">
<!ATTLIST doc tst:att (0 | 1) "1">
]>
<doc xmlns:tst="http://example.org" att="1"/>

6
result/noent/defattr.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc xmlns CDATA #FIXED "http://www.example.com/">
]>
<doc xmlns="http://www.example.com/"/>

View File

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc defatt (0 | 1) "0">
<!ATTLIST doc xmlns:tst CDATA #FIXED "http://example.org">
<!ATTLIST doc tst:att (0 | 1) "1">
]>
<doc xmlns:tst="http://example.org" att="1"/>

6
test/defattr.xml Normal file
View File

@ -0,0 +1,6 @@
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc
xmlns CDATA #FIXED "http://www.example.com/">
]>
<doc/>

8
test/defattr2.xml Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE doc [
<!ELEMENT doc EMPTY>
<!ATTLIST doc
defatt (0|1) "0"
xmlns:tst CDATA #FIXED "http://example.org"
tst:att (0|1) "1">
]>
<doc att="1"/>

View File

@ -175,6 +175,8 @@ int main(int argc, char **argv) {
usefile++;
}
if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
if (nocdata != 0) {
xmlDefaultSAXHandlerInit();
xmlDefaultSAXHandler.cdataBlock = NULL;

35
valid.c
View File

@ -1271,13 +1271,40 @@ xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
*/
elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
if (elemDef != NULL) {
if ((type == XML_ATTRIBUTE_ID) &&
(xmlScanIDAttributeDecl(NULL, elemDef) != 0))
VERROR(ctxt->userData,
"Element %s has too may ID attributes defined : %s\n",
elem, name);
ret->nexth = elemDef->attributes;
elemDef->attributes = ret;
/*
* Insert namespace default def first they need to be
* processed firt.
*/
if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
((ret->prefix != NULL &&
(xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
ret->nexth = elemDef->attributes;
elemDef->attributes = ret;
} else {
xmlAttributePtr tmp = elemDef->attributes;
while ((tmp != NULL) &&
((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
((ret->prefix != NULL &&
(xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
if (tmp->nexth == NULL)
break;
tmp = tmp->nexth;
}
if (tmp != NULL) {
ret->nexth = tmp->nexth;
tmp->nexth = ret;
} else {
ret->nexth = elemDef->attributes;
elemDef->attributes = ret;
}
}
}
/*
@ -2280,7 +2307,7 @@ xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
* returns the xmlElementPtr if found or NULL
*/
static xmlElementPtr
xmlElementPtr
xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
const xmlChar *prefix) {
xmlElementTablePtr table;
@ -2341,7 +2368,7 @@ xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
* returns the xmlAttributePtr if found or NULL
*/
static xmlAttributePtr
xmlAttributePtr
xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
const xmlChar *prefix) {
xmlAttributeTablePtr table;

View File

@ -97,6 +97,7 @@ static char *encoding = NULL;
#ifdef LIBXML_XINCLUDE_ENABLED
static int xinclude = 0;
#endif
static int dtdattrs = 0;
static int loaddtd = 0;
static int progresult = 0;
static int timing = 0;
@ -792,8 +793,9 @@ static void usage(const char *name) {
printf("\t--auto : generate a small doc on the fly\n");
#ifdef LIBXML_XINCLUDE_ENABLED
printf("\t--xinclude : do XInclude processing\n");
printf("\t--loaddtd : fetch external Dtd\n");
#endif
printf("\t--loaddtd : fetch external Dtd\n");
printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
}
int
main(int argc, char **argv) {
@ -850,7 +852,11 @@ main(int argc, char **argv) {
else if ((!strcmp(argv[i], "-loaddtd")) ||
(!strcmp(argv[i], "--loaddtd")))
loaddtd++;
else if ((!strcmp(argv[i], "-valid")) ||
else if ((!strcmp(argv[i], "-dtdattr")) ||
(!strcmp(argv[i], "--dtdattr"))) {
loaddtd++;
dtdattrs++;
} else if ((!strcmp(argv[i], "-valid")) ||
(!strcmp(argv[i], "--valid")))
valid++;
else if ((!strcmp(argv[i], "-postvalid")) ||
@ -946,7 +952,10 @@ main(int argc, char **argv) {
}
}
xmlLineNumbersDefault(1);
if (loaddtd != 0) xmlLoadExtDtdDefaultValue = 6; /* fetch DTDs by default */
if (loaddtd != 0)
xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
if (dtdattrs)
xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
if (noent != 0) xmlSubstituteEntitiesDefault(1);
if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
if ((htmlout) && (!nowrap)) {