@@ -84,7 +84,22 @@ public static IDictionary<XmlQualifiedName, string>
8484 public static XmlNode StripWhitespace ( XmlNode original ) {
8585 XmlNode cloned = original . CloneNode ( true ) ;
8686 cloned . Normalize ( ) ;
87- HandleWsRec ( cloned , false ) ;
87+ HandleWsRec ( cloned , TrimValue ) ;
88+ return cloned ;
89+ }
90+
91+ /// <summary>
92+ /// Creates a new Node (of the same type as the original node)
93+ /// that is similar to the orginal but doesn't contain any
94+ /// empty text or CDATA nodes and where all textual content
95+ /// including attribute values or comments are trimmed of
96+ /// characters XML considers whitespace according to
97+ /// <see href="https://www.w3.org/TR/xml11/#NT-S"/>.
98+ /// </summary>
99+ public static XmlNode StripXmlWhitespace ( XmlNode original ) {
100+ XmlNode cloned = original . CloneNode ( true ) ;
101+ cloned . Normalize ( ) ;
102+ HandleWsRec ( cloned , XmlTrimValue ) ;
88103 return cloned ;
89104 }
90105
@@ -104,7 +119,7 @@ public static XmlNode StripWhitespace(XmlNode original) {
104119 public static XmlNode NormalizeWhitespace ( XmlNode original ) {
105120 XmlNode cloned = original . CloneNode ( true ) ;
106121 cloned . Normalize ( ) ;
107- HandleWsRec ( cloned , true ) ;
122+ HandleWsRec ( cloned , TrimAndNormalizeValue ) ;
108123 return cloned ;
109124 }
110125
@@ -129,23 +144,44 @@ public static XmlNode StripElementContentWhitespace(XmlNode original) {
129144 return cloned ;
130145 }
131146
147+ /// <summary>
148+ /// Returns the nodes' value trimmed of all whitespace.
149+ /// <summary>
150+ private static String TrimValue ( XmlNode n ) {
151+ return n . Value . Trim ( ) ;
152+ }
153+
154+ /// <summary>
155+ /// Returns the nodes' value trimmed of all whitespace and Normalized
156+ /// <summary>
157+ private static String TrimAndNormalizeValue ( XmlNode n ) {
158+ return Normalize ( TrimValue ( n ) ) ;
159+ }
160+
161+ private static readonly char [ ] XML_WHITESPACE_CHARS = {
162+ ' ' , '\r ' , '\n ' , '\t '
163+ } ;
164+
165+ /// <summary>
166+ /// Returns the nodes' value trimmed of all characters XML considers whitespace.
167+ /// <summary>
168+ private static String XmlTrimValue ( XmlNode n ) {
169+ return n . Value . Trim ( XML_WHITESPACE_CHARS ) ;
170+ }
171+
132172 /// <summary>
133173 /// Trims textual content of this node, removes empty text and
134174 /// CDATA children, recurses into its child nodes.
135175 /// </summary>
136176 /// <parameter name="normalize">whether to normalize
137177 /// whitespace as well</parameter>
138- private static void HandleWsRec ( XmlNode n , bool normalize ) {
178+ private static void HandleWsRec ( XmlNode n , Func < XmlNode , String > handleWs ) {
139179 if ( n is XmlCharacterData || n is XmlProcessingInstruction ) {
140- string s = n . Value . Trim ( ) ;
141- if ( normalize ) {
142- s = Normalize ( s ) ;
143- }
144- n . Value = s ;
180+ n . Value = handleWs ( n ) ;
145181 }
146182 LinkedList < XmlNode > toRemove = new LinkedList < XmlNode > ( ) ;
147183 foreach ( XmlNode child in n . ChildNodes ) {
148- HandleWsRec ( child , normalize ) ;
184+ HandleWsRec ( child , handleWs ) ;
149185 if ( ! ( n is XmlAttribute )
150186 && IsTextualContentNode ( child )
151187 && child . Value . Length == 0 ) {
@@ -158,7 +194,7 @@ private static void HandleWsRec(XmlNode n, bool normalize) {
158194 XmlNamedNodeMap attrs = n . Attributes ;
159195 if ( attrs != null ) {
160196 foreach ( XmlAttribute a in attrs ) {
161- HandleWsRec ( a , normalize ) ;
197+ HandleWsRec ( a , handleWs ) ;
162198 }
163199 }
164200 }
0 commit comments