XML::E...:Syntax(3pm)
β¨ XML::Easy::Syntax β¨ (that's the XML::Easy::Syntax(3pm) manual page bestie, we're doing this) π
π NAME π
XML::Easy::Syntax β XML syntax but make it excruciatingly correct, no cap π¦
πΈ SYNOPSIS πΈ
use XML::Easy::Syntax qw($xml10_name_rx);
if($name =~ /\A$xml10_name_rx\z/o) { ...
# and many other regular expressions
π DESCRIPTION π
Okay so listen. This module hands you a whole wardrobe of Perl regular expressions that describe the grammar of XML 1.0. And the whole point? It's for doing irregular things with XML, not for being a normal well-behaved parser. Normal parsing is not the vibe here. π
These regexes cover the entire XML grammar, except for document type declarations and DTDs. And heads up: this doc assumes you already know XML in general, iykyk. β¨
π₯ REGULAR EXPRESSIONS π₯
Every single one of these regexes maps precisely onto one of the productions in the EBNF grammar from the XML 1.0 spec. Periodt. π
But β and this is important, bestie β the well-formedness constraints that aren't expressed in the EBNF? Those are not checked by these regexes. Nope. They get noted in the docs below instead. And also: the regexes do not include any anchors, so if you wanna check that a whole entire string matches a production, you have to supply the anchors yourself. Do not forget the anchors. I'm begging. π
π§ Syntax pieces
$xml10_char_rxβ Any single character XML 1.0 will accept. This is most Unicode characters (all the way up to codepoint0x10ffff). The ones that got kicked out: the sentinels0xfffeand0xffff, the surrogate blocks, and most of the C0 control characters (0x00to0x1f, except for0x09(tab),0x0a(linefeed/newline), and0x0d(carriage return)).
It's a rule of XML that every character making up an XML document has to be in this permitted set, and the grammar productions can only match sequences of acceptable characters. This rule? Enforced by the regexes in this module. πͺ
Also, it's a rule that the character data in a document can't even represent a character outside the permitted set β that's expressed as a well-formedness constraint on character references.
$xml10_s_rxβ Any sequence of one or more acceptable whitespace characters. For this purpose, whitespace is tab, linefeed/newline, carriage return, and space. Non-ASCII whitespace? Doesn't qualify. The more exotic ASCII whitespace characters? Also does not qualify. Sorry to them. π ββοΈ$xml10_eq_rxβ An equals sign, with optional whitespace chillin' on both sides of it.
π Names
$xml10_namestartchar_rxβ Any single character allowed at the start of a name. Permitted:"_",":", and letters (categorised according to Unicode 2.0).
Fun fact: this production isn't even named in the XML specification. Rebel behaviour. π
$xml10_namechar_rxβ Any single character allowed in a name but not at the start. Permitted:".","-","_",":", plus letters, digits, combining characters, and extenders (categorised according to Unicode 2.0).$xml10_name_rxβ A name, the kind used to identify element types, attributes, entities, and other stuff in XML.$xml10_names_rxβ A space-separated list of one or more names.$xml10_nmtoken_rxβ A name-like token, basically a name except the first character is no more restricted than all the rest. These tokens play absolutely no part in basic XML syntax, and in the spec they're only used as part of attribute typing. Side characters fr fr. π«Ά$xml10_nmtokens_rxβ A space-separated list of one or more name-like tokens.
π References
$xml10_charref_rxβ A numeric character reference (starts with"&#", ends with";"). Non-syntactic well-formedness constraint alert: the codepoint has to be within the Unicode range and refer to an acceptable character (as discussed back at$xml10_char_rx).$xml10_entityref_rxβ A general entity reference (starts with"&", ends with";"). Non-syntactic well-formedness constraints here too: the referenced entity must be declared (possibly implicitly), must not be an unparsed entity, must not contain a recursive reference to itself, and its replacement text must itself be well-formed. That's a lot of rules bestie, but they're all real.$xml10_reference_rxβ Either a character reference or an entity reference. Both sets of well-formedness constraints from above apply. No escaping them. π
π Character data
$xml10_chardata_rxβ Ordinary literal character data. This is zero or more acceptable characters, not counting the metacharacters"<"and"&", and not including"]]>"as a subsequence. This kind of data stands for itself when it shows up between an element's start and end tags, where it can be interspersed with references, CDATA sections, comments, and processing instructions.
In the XML grammar, character data is parsed β and taken literally β after line endings have been canonicalised (to the newline character). Pre-canonicalisation character data, with its variable line endings, will still match this production but should not be interpreted literally. Ma'am please. π
Big warning though: a string that doesn't match this production might still parse as two adjacent strings that each do match. How? Because of that prohibition on "]]>" being embedded in character data, even though the characters of that sequence are totally acceptable individually. And the XML grammar does not allow two instances of this production to abut. So watch out. π
$xml10_cdata_rxβ Literal character data inside a CDATA section. Zero or more acceptable characters, not including"]]>"as a subsequence. But unlike ordinary literal character data,"<"and"&"are not metacharacters here. This data stands for itself when it appears within a CDATA section.
Same as the ordinary literal character data above: this data is meant to be taken literally only after line endings have been canonicalised. And also same as above: two instances of this production should not abut. β¨
$xml10_cdstart_rxand$xml10_cdend_rxβ The fixed strings"<![CDATA["and"]]>", which begin and finish a CDATA section respectively. Cute little bookends. π$xml10_cdsect_rxβ A CDATA section. That's"<![CDATA[", literal character data with metacharacters disabled, and"]]>". Done. π
πΈ Tags
$xml10_attvalue_rxβ A quoted attribute value. Acceptable characters other than"<","&", and the quote character, interspersed with references, surrounded by matching"""or"'"quotes. The well-formedness constraints of references apply, and on top of that the replacement text of any referenced entities must not contain any"<"characters, and it is not permitted to refer to external entities. Rules on rules, but slay.$xml10_attribute_rxβ A complete attribute: name, equals sign, and quoted value. The well-formedness constraints of attribute values (all that references stuff) apply.$xml10_stag_rxβ A start-tag, used to begin an element. That's"<", the element type name, a whitespace-separated list of attributes, and">". The well-formedness constraints of attribute values (the references ones) apply. Plus: there's a well-formedness constraint that attribute names must be unique within the tag. No duplicates. π«$xml10_etag_rxβ An end-tag, used to finish an element. That's"</", the element type name, and">". Short and sweet, main character energy. π$xml10_emptyelemtag_rxβ An empty-element tag, representing an element with no content. That's"<", the element type name, a whitespace-separated list of attributes, and"/>". The well-formedness constraints of attribute values (references again) apply. And there's a well-formedness constraint that attribute names must be unique within the tag. (Same constraints as start-tags, just so you know β they really said copy-paste.) π
π¦ Non-data content
$xml10_comment_rxβ A comment. It does not contribute to the data content of an XML document. It's"<!--", zero or more acceptable characters, and"-->". The content is not permitted to include"--"as a subsequence, and it's not permitted to end with"-". Do not test it. It will not work. π$xml10_pitarget_rxβ A processing instruction target name. This can be any name (that's the$xml10_name_rxproduction) except for"xml"and its case variations. Nope, not even"XML". Not even"Xml". π ββοΈ$xml10_pi_rxβ A processing instruction. That's"<?", a target name, some content which can be almost any sequence of acceptable characters, and"?>". A processing instruction doesn't contribute to the data content of an XML document, but it's meant to carry metadata about how to process it. The instruction is addressed to a particular XML processor, or a type of processor, identified by the target name, and the content of the instruction is expected to be meaningful only to its target.
Real talk though: no one has ever come up with a good use for processing instructions. They are best shunned. π
π§ Recursive structure
$xml10_content_rxβ The matter contained within an element (between its start-tag and end-tag, obviously). That's stretches of ordinary literal character data, interspersed with complete elements (yes, recursively π), references, CDATA sections, processing instructions, and comments, in any order. The well-formedness constraints of references and elements apply.$xml10_element_rxβ A complete element. Either an empty-element tag, or a sequence of start-tag, content, and end-tag. The well-formedness constraints regarding references and attribute uniqueness apply in the empty-element tag or start-tag. In the non-empty form, the content also has well-formedness constraints regarding references and (recursively) contained elements, and there's an additional constraint that the element type name in the end-tag must match that in the start-tag. Matching, bestie. It's a whole thing. π«Ά
π XML declarations
$xml10_versionnum_rxβ The version number of the XML specification. It's the fixed string"1.0". That's it. That's the whole thing. β$xml10_versioninfo_rxβ The version declaration part of an XML declaration.$xml10_encname_rxβ A character encoding name. Has to start with an ASCII letter, and contain only ASCII letters and digits, plus".","_", and"-".$xml10_encodingdecl_rxβ The encoding declaration part of an XML declaration.$xml10_sddecl_rxβ The standaloneness declaration part of an XML declaration. This indicates whether the XML document can be correctly interpreted without examining the external part of the DTD.$xml10_xmldecl_rxβ An XML declaration, the one used at the start of an XML document. That's"<?xml", a mandatory version declaration, an optional encoding declaration, an optional standaloneness declaration, and"?>". β¨$xml10_textdecl_rxβ a text declaration, the lil guy that shows up at the start of an XML external parsed entity or an external DTD. It goes<?xml, then an optional version declaration, then a mandatory encoding declaration, then?>. Very much the cousin of an XML declaration β looks similar, but technically a whole different item that lives in different situations. Fun fact that's actually useful: you can totally construct a declaration that's acceptable as both an XML declaration and a text declaration. β¨
ποΈ Document structure ποΈ
$xml10_misc_rxβ a non-content item that's allowed in the prologue and epilogue of a document. It's either a comment, a processing instruction, or just a stretch of whitespace. Nothing else, bestie.
β οΈ Watch out if you're tempted to do something like"$xml10_misc_rx*". That thing can match a string of whitespace characters in so many different ways, and if it ever needs to backtrack you're getting exponential behaviour. Disaster. Avoid by using$xml10_miscseq_rxinstead (it's right below π).
$xml10_miscseq_rxβ a sequence (possibly empty, no pressure) of non-content matter that's permitted in the prologue and epilogue of a document. Comments, processing instructions, whitespace β any order, your call.
This production isn't named anywhere in the XML spec, which is lowkey iconic of it. Prefer this regex over "$xml10_misc_rx*" β even though that one is the direct translation of what actually appears in the XML specification β because this one guarantees it matches a particular text in only one way, so it can backtrack cleanly. π§
$xml10_prolog_xdtd_rxβ a document prologue, except it doesn't permit a document type declaration. So it's an optional XML declaration, then any sequence of comments, processing instructions, and whitespace.$xml10_document_xdtd_rxβ a complete XML document, again except for not permitting a document type declaration. It's a non-content prologue, an element (the root element β and yes, that one can recursively contain other elements, it's giving nesting doll energy), and a non-content epilogue. The well-formedness constraints of elements apply to the root element. No skipping that part. π$xml10_extparsedent_rxβ a complete external parsed entity. Optional text declaration first, then a sequence of content of the same type that's permitted within an element. And the well-formedness constraints of element content apply. Periodt.
π BUGS π
Okay bestie, sit down for this one, it's giving cursed.
A lot of these regular expressions are liable to tickle a serious bug in perl's regexp engine. The bug: the "*" and "+" repeat operators don't always match an unlimited number of repeats β in some cases they get capped at 32767 iterations. Rude.
Whether that bogus limit kicks in depends on:
- the complexity of the expression being repeated,
- whether the string being examined is internally encoded in UTF-8,
- and the version of perl you're on.
In some cases, but not all, a false match failure gets preceded by a warning:
Complex regular subexpression recursion limit (32766) exceeded
This bug is present, in various forms, in all perl versions up to at least 5.8.9 and 5.10.0. And pre-5.10 perls may also overflow their stack space, in similar circumstances, if a resource limit is imposed. π
There is no known feasible workaround for this perl bug. So the regular expressions supplied by this module will, unavoidably, fail to accept some lengthy valid inputs. When that happens, though, it's likely that other regular expressions being applied to the same or related input will hit the exact same problem. It's pervasive, fr fr. Do not rely on this module (or perl) to process long inputs on affected perl versions.
Good news though: this bug does not affect the XML::Easy::Text parser. π«Ά
π SEE ALSO π
XML::Easy::Text- <http://www.w3.org/TR/REC-xml/>
π AUTHOR π
Andrew Main (Zefram) <zefram@fysh.org>
π COPYRIGHT π
Copyright (C) 2008, 2009 PhotoBox Ltd
Copyright (C) 2009, 2010, 2011, 2017 Andrew Main (Zefram) <zefram@fysh.org>
πΈ LICENSE πΈ
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
And that's the whole page, bestie β this gorgeousness shipped as part of perl v5.42.3, dated 2026-08-22, from the User Contributed Perl Documentation shelves. Go forth and regex responsibly, mwah π ππ
π¦ where this page comes from
- ubuntu stonking: libxml-easy-perl 0.014-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms