Converting XML to plain text using XSLT — how should I ignore/handle whitespace in the XSLT?

There are three reasons for getting unwanted whitespace in the result of an XSLT transformation.

There are three reasons for getting unwanted whitespace in the result of an XSLT transformation: whitespace that comes from between nodes in the source document whitespace that comes from within nodes in the source document whitespace that comes from the stylesheet I'm going to talk about all three because it can be hard to tell where whitespace comes from so you might need to use several strategies. To address the whitespace that is between nodes in your source document, you should use to strip out any whitespace that appears between two nodes, and then use to preserve the significant whitespace that might appear within mixed content. For example, if your source document looks like: This is an important point then you will want to ignore the whitespace between the and the and between the and the , which is not significant, but preserve the whitespace between the and elements, which is significant (otherwise you'd get "This is an important*point*").

To do this use The elements attribute on should basically list all the elements in your document that have mixed content. Aside: using also reduces the size of the source tree in memory, and makes your stylesheet more efficient, so it's worth doing even if you don't have whitespace problems of this sort. To address the whitespace that appears within nodes in your source document, you should use normalize-space().

For example, if you have: a definition and you can be sure that the element won't hold any elements that you want to do something with, then you can do: ... ... The leading and trailing whitespace will be stripped from the value of the element and you will just get the string "a definition". To address whitespace coming from the stylesheet, which is perhaps the one you're experiencing, is when you have text within a template like this: Name: XSLT stylesheets are parsed in the same way as the source documents that they process, so the above XSLT is interpreted as a tree that holds an element with a match attribute whose first child is a text node and whose second child is a element with a select attribute. The text node has leading and trailing whitespace (including line breaks); since it's literal text in the stylesheet, it gets literally copied over into the result, with all the leading and trailing whitespace.

But some whitespace in XSLT stylesheets get stripped automatically, namely those between nodes. You don't get a line break in your result because there's a line break between the and the close of the . To get only the text you want in the result, use the element like this: Name: The XSLT processor will ignore the line breaks and indentation that appear between nodes, and only output the text within the element.

This was extremely helpful! Thanks. – Black Dec 17 '08 at 11:59 that was indeed helpful, but I'm puzzled by your use of the phrase "between nodes".

Isn't it true that all whitespace is contained in text nodes? What do you mean by "between nodes"? If I hadn't recognized your name I would have assumed you needed a lecture on XML document structure.

– LarsH Sep 5 '10 at 1:58 Good article, thanks! But strictly speaking, you're using the term 'node' where you actually mean 'element'. – rustyx Jan 5 at 18:50 @LarsH: I'm outside of my domain here (and a few months late), but I think this answers your question: w3.Org/TR/xslt#strip "...some text nodes are stripped.

A text node is never stripped unless it contains only whitespace characters. " "A text node is preserved if ... the text node contains at least one non-whitespace character." – Dan Jan 16 at 4:08.

Also if you're using xsl:value-of you can use the disable-output-escaping="yes" to help with some whitespace issues.

2 Most of the time, using disable-output-escaping is the wrong way to do things. It's only there for very restricted situations. Advocating d-o-e in such a general way to someone who doesn't know better is probably more harmful than helpful.

See dpawson.co. Uk/xsl/sect2/N2215. Html#d3702e223 – LarsH Sep 5 '10 at 1:51.

JeniT's answer is great, I just want to point out a trick for managing whitespace. I'm not certain it's the best way (or even a good way), but it works for me for now. ("s" for space, "e" for empty, "n" for newline.) " > " > " > " > " > " > > &e;Flush left, despite the indentation.

&n; &e; This line will be output indented two spaces. &n; &e; Starts with two blanks: . &n; &e; The 'e' trick won't work here.

&n; &s2; Use s2 instead. &n; &s2; &n; &s2; &s;&n; Applied to: Outputs: Flush left, despite the indentation. This line will be output indented two spaces.

Starts with two blanks: bar. Baz The 'e' trick won't work here. Baz Use s2 instead.

Abcxyz abc xyz The 'e' trick works prior to a text node containing at least one non-whitespace character because it expands to this: Flush left, despite the indentation. Since the rules for stripping whitespace say that whitespace-only text nodes get stripped, the newline and indentation between the and get stripped (good). Since the rules say a text node with at least one whitespace character is preserved, the implicit text node containing " This line will be output indented two spaces.

" keeps its leading whitespace (but I guess this also depends on the settings for strip/preserve/normalize). The "&n;" at the end of the line inserts a newline, but it also ensures that any following whitespace is ignored, because it appears between two nodes. The trouble I have is when I want to output an indented line that begins with an .

In that case, the "&e;" won't help, because the indentation whitespace isn't "attached" to any non-whitespace characters. So for those cases, I use "&s2;" or "&s4;", depending on how much indentation I want.It's an ugly hack I'm sure, but at least I don't have the verbose "" tags littering my XSLT, and at least I can still indent the XSLT itself so it's legible. I feel like I'm abusing XSLT for something it was not designed for (text processing) and this is the best I can do.

Edit: In response to comments, this is what it looks like without the "macros": Flush left, despite the indentation. This line will be output indented two spaces. Starts with two blanks: .

I think that makes it less clear to see the intended output indentation, and it screws up the indentation of the XSL itself because the end tags have to appear at column 1 of the XSL file (otherwise you get undesired whitespace in the output file).

Dan: First, xsl:text it's not verbose, and you always can use concat on xsl:value-of. Second, you are not processing text, your output is plain text. – user357812 Jan 17 at 16:10 @Dan: Last.

Your solution is against XSLT because those entities (properly declared) are part of the surface syntax of the XML document (the stylesheet, in this case). So, the replacement takes time in the parsing fase, before reaching the XSLT processor. Once the replace was performed and there are new elements in the stylesheet, the rules for stripping/preserving whitespace only text nodes are applied.

From a reader's point of view, it won't be clear what would be your stylesheet result. – user357812 Jan 17 at 16:12 @Alejandro: thanks for the feedback. I suppose it's not verbose if you're already accustomed to XML... my background is more lex/yacc/C++ so I'm definitely feeling out of my element here.

I suppose using an XML editor vs. a text editor might help. – Dan Jan 17 at 17:39 @Alejandro: regarding whether it's clear or not... I guess that's a matter of opinion. Either using xsl:text or the &e; type "macros" is better than what was proposed as an alternative in the question: "delete about 95% of the whitespace from the XSL file, making it nigh-unreadable and a maintenance nightmare." – Dan Jan 17 at 17:41 @Dan: What shows that it's not a matter of opinion is the need of &s2; instead of &e; in some cases, for the same effect.

– user357812 Jan 17 at 18:35.

Regarding your edit about new lines, you can use this template to recursively replace one string within another string, and you can use it for line breaks: Call it as follows (this example replaces line breaks in the $some. String variable with a space).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions