Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add line break into text that I am creating with XSLT?

Tags:

xml

xslt

I am trying to create text output from an xml file using xslt. It is actually an xslt that creates SQL code. Here is a part that outputs CREATE TABLE statements:

CREATE TABLE dbo.[<xsl:value-of select="@PhysicalName"/>] (
  <xsl:for-each select="EntityAttributes/EntityAttribute">
    <xsl:apply-templates select="Attributes/Attribute[@AttributeID = current()/@EntityAttributeID]"/> ...
  </xsl:for-each>)

I want to have a line break after the "(" in the first line but cannot manage to find out how to do so. Can anyone help?

like image 431
bjorsig Avatar asked Jan 25 '11 22:01

bjorsig


People also ask

How do I add a line break in XSLT?

print("\n\n") to put line breaks exactly where we need them. Get XSLT, 2nd Edition now with the O'Reilly learning platform.

How do I add a new line in XML?

You can use &#xA; for line feed (LF) or &#xD; for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application.

How do you break out of a loop in XSLT?

There is no such a thing as break in an XSLT for-each loop. xsl:if/@test is pretty much all you can do. The other way would be to include this condition within the for-each/@select.

What is text () in XSLT?

The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

As for line breaks, I myself prefer more explicit/readable way.

<xsl:text>&#xa;</xsl:text>
like image 56
Flack Avatar answered Oct 11 '22 21:10

Flack


If you put

<xsl:text>
</xsl:text>

in your XSLT it will give a line break. It is not clear where you wish to put this. I am guessing you want:

<xsl:text>CREATE TABLE dbo.[</xsl:text><xsl:value-of select="@PhysicalName"/><xsl:text>] (
</xsl:text>

In general you should always wrap text output in ... it looks slightly horrid in the XSL but it preserves the spacing. Note that you can break the lines in the XSLT without affecting the result - e.g.

<xsl:text>CREATE TABLE dbo.[</xsl:text>
<xsl:value-of select="@PhysicalName"/>
<xsl:text>] (&#xa;</xsl:text>

and yes, I agree about the explicit line break character. As you can see the XSLT is not very readable but it gets the right answer

like image 45
peter.murray.rust Avatar answered Oct 11 '22 22:10

peter.murray.rust