I want to produce a newline for text output in XSLT. Any ideas?
相关问题
- XML - XSLT - document() function inside count() fu
- Using XSLT to select after EACH instance in a stri
- XSLT open other xml files
- How to use MSbuild property in TransformXml task?
- Using asp objects in .NET - Max compatibility
相关文章
- xslt localization
- convert xml to soap request using xslt transformat
- XALAN register Extension Function like in SAXON
- How to group using XSLT
- How to reformat XML with group-adjacent (XSLT)
- AddExtensionObject - Performance
- Transforming HTML nodes with XSLT in chrome/webkit
- visual studio 2015 xsl debugging transformation fa
just add this tag:
it works for me ;) .
I added the
DOCTYPE
directive you see here:This allows me to use
&nl;
instead of

to produce a newline in the output. Like other solutions, this is typically placed inside a<xsl:text>
tag.I have found a difference between literal newlines in
<xsl:text>
and literal newlines using

.While literal newlines worked fine in my environment (using both Saxon and the default Java XSLT processor) my code failed when it was executed by another group running in a .NET environment.
Changing to entities (


) got my file generation code running consistently on both Java and .NET.Also, literal newlines are vulnerable to being reformatted by IDEs and can inadvertently get lost when the file is maintained by someone 'not in the know'.
You can use:
<xsl:text> </xsl:text>
see the example
if you write this in file e.g.
this variable will produce a new line infile as:
IMHO no more info than @Florjon gave is needed. Maybe some small details are left to understand why it might not work for us sometimes.
First of all, the


(hex) or

(dec) inside a<xsl:text/>
will always work, but you may not see it.<br/>
will do fine. Otherwise you'll see a white space. Viewing the source from the browser will tell you what really happened. However, there are cases you expect this behaviour, especially if the consumer is not directly a browser. For instance, you want to create an HTML page and view its structure formatted nicely with empty lines and idents before serving it to the browser.disable-output-escaping
and where you don't. Take the following example where I had to create an xml from another and declare its DTD from a stylesheet.The first version does escape the characters (default for xsl:text)
and here is the result:
Ok, it does what we expect, escaping is done so that the characters we used are displayed properly. The XML part formatting inside the root node is handled by
ident="yes"
. But with a closer look we see that the newline character

was not escaped and translated as is, performing a double linefeed! I don't have an explanation on this, will be good to know. Anyone?The second version does not escape the characters so they're producing what they're meant for. The change made was:
and here is the result:
and that will be ok. Both cr and lf are properly rendered.
nl
, notcrlf
(nl=lf
). My first attempt was to use only cr:
and while the output xml was validated by DOM properly.I was viewing a corrupted xml:
DOM parser disregarded control characters but the rendered didn't. I spent quite some time bumping my head before I realised how silly I was not seeing this!
For the record, I do use a variable inside the body with both CRLF just to be 100% sure it will work everywhere.
Include the attribute Method="text" on the xsl:output tag and include newlines in your literal content in the XSL at the appropriate points. If you prefer to keep the source code of your XSL tidy use the entity
where you want a new line.