Eagle
Eagle

Reputation: 3474

XSLT split content of line over multi lines

I have in my XSLT file the following line:

  <init><xsl:value-of select="fn:string-join(__something__),',')"/></init>

where something is not the real content.

At the moment, the above line generate lines which exceed the size of 120 chars.

<init>1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000</init>

I would like that my output to look as following:

<init>1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
1000</init>

My Qestions:

  1. How can i tell XSLT to limit my line size to 120 chars?
  2. How could i make sure the numbers string won't be splited?

where, a bad example (The second number is splited in to two parts):

`<init>1000,10
       00,1000,1</init>`

good example:

 `<init>1000,1000,
       1000,1
   </init>`

Upvotes: 0

Views: 963

Answers (2)

lfurini
lfurini

Reputation: 3788

(Premise: as someone has already given the answer to your precise question, I'll risk going off-topic with an answer for a slightly different problem)

Unlike adding indentation to elements, inserting newline characters in the middle of a text node is not just a cosmetic operation, a matter of presentation and nothing more: the length and string value of the node is changed.

If your output xml is in turn the input of another transformation, that xslt must take into account the added newlines, or string functions like tokenize, ends-with, contains, etc. could give unexpected results.

In conclusion, if you are looking for a way to see the lines of your result properly wrapped I would suggest using fn:string-join(__something__),', ') (with a space after the comma) to concatenate your values, and let your code editor wrap the lines as needed.

The difference is that these spaces have been added uniformly after each value, instead of "here and there" according to the word lengths, and can be easily taken into account for successive transformation without having to use regular expressions.

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122364

You could use the replace function along these lines:

<init><xsl:value-of select="replace(string-join(__something__),','),
                                    '.{0,119}(,|$)', '$0&#10;')"/></init>

The regular expression .{0,119}(,|$) will match as many characters as it can up to 120 finishing with either a comma or the end of the string, and the replacement string $0&#10; will add a newline following that comma (or the end of the string). If you don't want the extra newline at the very end you could wrap the whole thing in replace(..., '\n$', '') to strip it back off again.

Upvotes: 3

Related Questions