I need a regex or a function in PHP that will validate a string to be a good XML element name.
Form w3schools:
XML elements must follow these
XML, xml and etc are valid tags, they are just "reserved for standardization in this or future versions of this specification" which likely will never happen. Please check the real standard at https://www.w3.org/TR/REC-xml/. The w3school article is inaccurate.
The expression below should match valid unicode element names excepting xml. Names that start or end with xml will still be allowed. This passes @toscho's äøñ test. The one thing I could not figure out a regex for was extenders. The xml element name spec says:
[4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender
[5] Name ::= (Letter | '_' | ':') (NameChar)*
But there's no clear definition for a unicode category or class containing extenders.
^[\p{L}_:][\p{N}\p{L}\p{Mc}.\-|:]*((?<!xml)|xml)$
This has been missed so far despite the fact the question is that old: Name validation via PHP's pcre functions that are streamlined with the XML specification.
XML's definition is pretty clear about the element name in it's specs (Extensible Markup Language (XML) 1.0 (Fifth Edition)):
[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5] Name ::= NameStartChar (NameChar)*
This notation can be transposed into a UTF-8 compatible regular expression to be used with preg_match, here as single-quoted PHP string to be copied verbatim:
'~^[:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}][:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}]*$~u'
Or as another variant with named subpatterns in a more readable fashion:
'~
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
(?(DEFINE)
(?<NameStartChar> [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}])
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
(?<Name> (?&NameStartChar) (?&NameChar)*)
)
^(?&Name)$
~ux'
Note that this pattern contains the colon :
which you might want to exclude (two appereances in the first pattern, one in the second) for XML Namespace validation reasons (e.g. a test for NCName).
Usage Example:
$name = '::...';
$pattern = '~
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
(?(DEFINE)
(?<NameStartChar> [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}])
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
(?<Name> (?&NameStartChar) (?&NameChar)*)
)
^(?&Name)$
~ux';
$valid = 1 === preg_match($pattern, $name); # bool(true)
The saying that an element name starting with XML
(in lower or uppercase letters) would not be possible is not correct. <XML/>
is a perfectly well-formed XML and XML
is a perfectly well-formed element name.
It is just that such names are in the subset of well-formed element names that are reserved for standardization (XML version 1.0 and above). It is easy to test if a (well-formed) element name is reserved with a string comparison:
$reserved = $valid && 0 === stripos($name, 'xml'));
or alternatively another regular expression:
$reserved = $valid && 1 === preg_match('~^[Xx][Mm][Ll]~', $name);
PHP's DOMDocument can not test for reserved names at least I don't know any way how to do that and I've been looking a lot.
A valid element name needs a Unique Element Type Declaration which seems to be out of the scope of the question here as no such declaration has been provided. Therefore the answer does not take care of that. If there would be an element type declaration, you would only need to validate against a white-list of all (case-sensitive) names, so this would be a simple case-sensitive string-comparison.
Excursion: What does DOMDocument
do different to the Regular Expression?
In comparison with a DOMDocument
/ DOMElement
, there are some differences what qualifies a valid element name. The DOM extension is in some kind of mixed-mode which makes it less predictable what it validates. The following excursion illustrates the behavior and shows how to control it.
Let's take $name
and instantiate an element:
$element = new DOMElement($name);
The outcome depends:
So the first character decides about the comparison mode.
A regular expression is specifically written what to check for, here the XML 1.0 Name
symbol.
You can achieve the same with DOMElement
by prefixing the name with a colon:
function isValidXmlName($name)
{
try {
new DOMElement(":$name");
return TRUE;
} catch (DOMException $e) {
return FALSE;
}
}
To explicitly check for the QName
this can be achieved by turning it into a PrefixedName
in case it is a UnprefixedName
:
function isValidXmlnsQname($qname)
{
$prefixedName = (!strpos($qname, ':') ? 'prefix:' : '') . $qname;
try {
new DOMElement($prefixedName, NULL, 'uri:ns');
return TRUE;
} catch (DOMException $e) {
return FALSE;
}
}
This should give you roughly what you need [Assuming you are using Unicode]:
(Note: This is completely untested.)
[^\p{P}xX0-9][^mMlL\s]{2}[\w\p{P}0-9-]
\p{P} is the syntax for Unicode Punctuation marks in PHP's regular expression syntax.
Use this regex:
^_?(?!(xml|[_\d\W]))([\w.-]+)$
This matches all your four points and allows unicode characters.
if (substr(strtolower($text), 0, 3) != 'xml') && (1 === preg_match('/^\w[^<>]+$/', $text)))
{
// valid;
}