Skip to content

Commit c091819

Browse files
committed
Add schema default/fixed value support
Added support for adding fixed/default values during XSD validation and added/updated associated tests
1 parent a5cfe57 commit c091819

11 files changed

+132
-10
lines changed

ext/dom/document.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,14 +1973,15 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
19731973
xmlDoc *docp;
19741974
dom_object *intern;
19751975
char *source = NULL, *valid_file = NULL;
1976-
int source_len = 0;
1976+
int source_len = 0, valid_opts = 0;
1977+
long flags = 0;
19771978
xmlSchemaParserCtxtPtr parser;
19781979
xmlSchemaPtr sptr;
19791980
xmlSchemaValidCtxtPtr vptr;
19801981
int is_valid;
19811982
char resolved_path[MAXPATHLEN + 1];
19821983

1983-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Op", &id, dom_document_class_entry, &source, &source_len) == FAILURE) {
1984+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Op|l", &id, dom_document_class_entry, &source, &source_len, &flags) == FAILURE) {
19841985
return;
19851986
}
19861987

@@ -2029,6 +2030,13 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
20292030
RETURN_FALSE;
20302031
}
20312032

2033+
#if LIBXML_VERSION >= 20614
2034+
if (flags & XML_SCHEMA_VAL_VC_I_CREATE) {
2035+
valid_opts |= XML_SCHEMA_VAL_VC_I_CREATE;
2036+
}
2037+
#endif
2038+
2039+
xmlSchemaSetValidOptions(vptr, valid_opts);
20322040
xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
20332041
is_valid = xmlSchemaValidateDoc(vptr, docp);
20342042
xmlSchemaFree(sptr);
@@ -2042,14 +2050,14 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
20422050
}
20432051
/* }}} */
20442052

2045-
/* {{{ proto boolean dom_document_schema_validate_file(string filename); */
2053+
/* {{{ proto boolean dom_document_schema_validate_file(string filename, int flags); */
20462054
PHP_FUNCTION(dom_document_schema_validate_file)
20472055
{
20482056
_dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_FILE);
20492057
}
20502058
/* }}} end dom_document_schema_validate_file */
20512059

2052-
/* {{{ proto boolean dom_document_schema_validate(string source); */
2060+
/* {{{ proto boolean dom_document_schema_validate(string source, int flags); */
20532061
PHP_FUNCTION(dom_document_schema_validate_xml)
20542062
{
20552063
_dom_document_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, DOM_LOAD_STRING);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
DomDocument::schemaValidateSource() - Add missing attribute default values from schema
3+
--CREDITS--
4+
Chris Wright <[email protected]>
5+
--SKIPIF--
6+
<?php require_once('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
$doc = new DOMDocument;
11+
12+
$doc->load(dirname(__FILE__)."/book-attr.xml");
13+
14+
$xsd = file_get_contents(dirname(__FILE__)."/book.xsd");
15+
16+
$doc->schemaValidateSource($xsd, LIBXML_SCHEMA_CREATE);
17+
18+
foreach ($doc->getElementsByTagName('book') as $book) {
19+
var_dump($book->getAttribute('is-hardback'));
20+
}
21+
22+
?>
23+
--EXPECT--
24+
string(5) "false"
25+
string(4) "true"

ext/dom/tests/DOMDocument_schemaValidateSource_error4.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ var_dump($result);
1717

1818
?>
1919
--EXPECTF--
20-
Warning: DOMDocument::schemaValidateSource() expects exactly 1 parameter, 0 given in %s.php on line %d
20+
Warning: DOMDocument::schemaValidateSource() expects at least 1 parameter, 0 given in %s.php on line %d
2121
NULL
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
DomDocument::schemaValidateSource() - Don't add missing attribute default values from schema
3+
--CREDITS--
4+
Chris Wright <[email protected]>
5+
--SKIPIF--
6+
<?php require_once('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
$doc = new DOMDocument;
11+
12+
$doc->load(dirname(__FILE__)."/book-attr.xml");
13+
14+
$xsd = file_get_contents(dirname(__FILE__)."/book.xsd");
15+
16+
$doc->schemaValidateSource($xsd);
17+
18+
foreach ($doc->getElementsByTagName('book') as $book) {
19+
var_dump($book->getAttribute('is-hardback'));
20+
}
21+
22+
?>
23+
--EXPECT--
24+
string(0) ""
25+
string(4) "true"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
DomDocument::schemaValidate() - Add missing attribute default values from schema
3+
--CREDITS--
4+
Chris Wright <[email protected]>
5+
--SKIPIF--
6+
<?php require_once('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
$doc = new DOMDocument;
11+
12+
$doc->load(dirname(__FILE__)."/book-attr.xml");
13+
14+
$doc->schemaValidate(dirname(__FILE__)."/book.xsd", LIBXML_SCHEMA_CREATE);
15+
16+
foreach ($doc->getElementsByTagName('book') as $book) {
17+
var_dump($book->getAttribute('is-hardback'));
18+
}
19+
20+
?>
21+
--EXPECT--
22+
string(5) "false"
23+
string(4) "true"

ext/dom/tests/DOMDocument_schemaValidate_error4.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ var_dump($result);
1717

1818
?>
1919
--EXPECTF--
20-
Warning: DOMDocument::schemaValidate() expects exactly 1 parameter, 0 given in %s.php on line %d
20+
Warning: DOMDocument::schemaValidate() expects at least 1 parameter, 0 given in %s.php on line %d
2121
NULL

ext/dom/tests/DOMDocument_schemaValidate_error5.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
DomDocument::schemaValidate() - non-existant schema file
2+
DomDocument::schemaValidate() - non-existent schema file
33
--CREDITS--
44
Daniel Convissor <[email protected]>
55
# TestFest 2009 NYPHP
@@ -12,14 +12,14 @@ $doc = new DOMDocument;
1212

1313
$doc->load(dirname(__FILE__)."/book.xml");
1414

15-
$result = $doc->schemaValidate(dirname(__FILE__)."/non-existant-file");
15+
$result = $doc->schemaValidate(dirname(__FILE__)."/non-existent-file");
1616
var_dump($result);
1717

1818
?>
1919
--EXPECTF--
20-
Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "%snon-existant-file" in %s.php on line %d
20+
Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "%snon-existent-file" in %s.php on line %d
2121

22-
Warning: DOMDocument::schemaValidate(): Failed to locate the main schema resource at '%s/non-existant-file'. in %s.php on line %d
22+
Warning: DOMDocument::schemaValidate(): Failed to locate the main schema resource at '%s/non-existent-file'. in %s.php on line %d
2323

2424
Warning: DOMDocument::schemaValidate(): Invalid Schema in %s.php on line %d
2525
bool(false)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
DomDocument::schemaValidate() - Don't add missing attribute default values from schema
3+
--CREDITS--
4+
Chris Wright <[email protected]>
5+
--SKIPIF--
6+
<?php require_once('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
10+
$doc = new DOMDocument;
11+
12+
$doc->load(dirname(__FILE__)."/book-attr.xml");
13+
14+
$doc->schemaValidate(dirname(__FILE__)."/book.xsd");
15+
16+
foreach ($doc->getElementsByTagName('book') as $book) {
17+
var_dump($book->getAttribute('is-hardback'));
18+
}
19+
20+
?>
21+
--EXPECT--
22+
string(0) ""
23+
string(4) "true"

ext/dom/tests/book-attr.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<books>
3+
<book>
4+
<title>The Grapes of Wrath</title>
5+
<author>John Steinbeck</author>
6+
</book>
7+
<book is-hardback="true">
8+
<title>The Pearl</title>
9+
<author>John Steinbeck</author>
10+
</book>
11+
</books>

ext/dom/tests/book.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<xs:element name="title" type="xs:string"/>
1010
<xs:element name="author" type="xs:string"/>
1111
</xs:sequence>
12+
<xs:attribute name="is-hardback" type="xs:boolean" default="false" use="optional" />
1213
</xs:complexType>
1314
</xs:element>
1415
</xs:sequence>

ext/libxml/libxml.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <libxml/xmlsave.h>
4545
#ifdef LIBXML_SCHEMAS_ENABLED
4646
#include <libxml/relaxng.h>
47+
#include <libxml/xmlschemas.h>
4748
#endif
4849

4950
#include "php_libxml.h"
@@ -798,6 +799,11 @@ static PHP_MINIT_FUNCTION(libxml)
798799
#endif
799800
REGISTER_LONG_CONSTANT("LIBXML_NOEMPTYTAG", LIBXML_SAVE_NOEMPTYTAG, CONST_CS | CONST_PERSISTENT);
800801

802+
/* Schema validation options */
803+
#if defined(LIBXML_SCHEMAS_ENABLED) && LIBXML_VERSION >= 20614
804+
REGISTER_LONG_CONSTANT("LIBXML_SCHEMA_CREATE", XML_SCHEMA_VAL_VC_I_CREATE, CONST_CS | CONST_PERSISTENT);
805+
#endif
806+
801807
/* Additional constants for use with loading html */
802808
#if LIBXML_VERSION >= 20707
803809
REGISTER_LONG_CONSTANT("LIBXML_HTML_NOIMPLIED", HTML_PARSE_NOIMPLIED, CONST_CS | CONST_PERSISTENT);

0 commit comments

Comments
 (0)