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?
print("\n\n") to put line breaks exactly where we need them. Get XSLT, 2nd Edition now with the O'Reilly learning platform.
You can use 
 for line feed (LF) or 
 for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application.
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.
The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
As for line breaks, I myself prefer more explicit/readable way.
<xsl:text>
</xsl:text>
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>] (
</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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With