A perlsax filter that detects ignorable whitespace
use XML::Filter::DetectWS; my $detect = new XML::Filter::DetectWS (Handler => $handler, SkipIgnorableWS => 1);
This a PerlSAX filter that detects which character data contains ignorable whitespace and optionally filters it.
Note that this is just a first stab at the implementation and it may change completely in the near future. Please provide feedback whether you like it or not, so I know whether I should change it.
The \s-1XML\s0 spec defines ignorable whitespace as the character data found in elements that were defined in an <!ELEMENT> declaration with a model of '\s-1EMPTY\s0' or 'Children' (Children is the rule that does not contain '#PCDATA'.)
In addition, XML::Filter::DetectWS allows the user to define other whitespace to be ignorable. The ignorable whitespace is passed to the PerlSAX Handler with the ignorable_whitespace handler, provided that the Handler implements this method. (Otherwise it is passed to the characters handler.) If the SkipIgnorableWS is set, the ignorable whitespace is simply discarded.
XML::Filter::DetectWS also takes xml:space attributes into account. See below for details.
\s-1CDATA\s0 sections are passed in the standard PerlSAX way (i.e. with surrounding start_cdata and end_cdata events), unless the Handler does not implement these methods. In that case, the \s-1CDATA\s0 section is simply passed to the characters method.
SkipIgnorableWS (Default: 0) When set, detected ignorable whitespace is discarded.
Handler The PerlSAX handler (or filter) that will receive the PerlSAX events from this filter.
When determining which whitespace is ignorable, it first looks at the xml:space attribute of the parent element node (and its ancestors.) If the attribute value is \*(L"preserve\*(R", then it is *NOT* ignorable. (If someone took the trouble of adding xml:space=\*(L"preserve\*(R", then that is the final answer...)
If xml:space=\*(L"default\*(R", then we look at the <!ELEMENT> definition of the parent element. If the model is '\s-1EMPTY\s0' or follows the 'Children' rule (i.e. does not contain '#PCDATA') then we know that the whitespace is ignorable. Otherwise we need input from the user somehow.
The idea is that the \s-1API\s0 of DetectWS will be extended, so that you can specify/override e.g. which elements should behave as if xml:space=\*(L"preserve\*(R" were set, and/or which elements should behave as if the <!ELEMENT> model was defined a certain way, etc.
Please send feedback!
The current implementation also detects whitespace after an element-start tag, whitespace before an element-end tag. It also detects whitespace before an element-start and after an element-end tag and before or after comments, processing instruction, cdata sections etc., but this needs to be reimplemented. In either case, the detected whitespace is split off into its own PerlSAX characters event and an extra property 'Loc' is added. It can have 4 possible values:
1 (\s-1WS_START\s0) - whitespace immediately after element-start tag
2 (\s-1WS_END\s0) - whitespace just before element-end tag
3 (\s-1WS_ONLY\s0) - both \s-1WS_START\s0 and \s-1WS_END\s0, i.e. it's the only text found between the start and end tag and it's all whitespace
0 (\s-1WS_INTER\s0) - none of the above, probably before an element-start tag, after an element-end tag, or before or after a comment, \s-1PI\s0, cdata section etc.
Note that \s-1WS_INTER\s0 may not be that useful, so this may change.
The \s-1XML\s0 spec states that: A special attribute named xml:space may be attached to an element to signal an intention that in that element, white space should be preserved by applications. In valid documents, this attribute, like any other, must be declared if it is used. When declared, it must be given as an enumerated type whose only possible values are \*(L"default\*(R" and \*(L"preserve\*(R". For example:
<!ATTLIST poem xml:space (default|preserve) 'preserve'>
The value \*(L"default\*(R" signals that applications' default white-space processing modes are acceptable for this element; the value \*(L"preserve\*(R" indicates the intent that applications preserve all the white space. This declared intent is considered to apply to all elements within the content of the element where it is specified, unless overriden with another instance of the xml:space attribute.
The root element of any document is considered to have signaled no intentions as regards application space handling, unless it provides a value for this attribute or the attribute is declared with a default value.
[... end of excerpt ...]
This code is highly experimental! It has not been tested well and the \s-1API\s0 may change.
The code that detects of blocks of whitespace at potential indent positions may need some work.
Enno Derksen is the Original Author.
Send bug reports, hints, tips, suggestions to T.J. Mather at <[email protected]>.