Project

Profile

Help

CDATA and result-document compatibility

Added by Anonymous over 17 years ago

Legacy ID: #4015728 Legacy Poster: pbodu (pbodu)

Hi there, I'm getting familiar with XSLT and the Saxon-B implementation of xslt 2.0 to generate C code files from xml files. I'm trying to get clean xslt AND C code. I use <xsl:strip-space elements=""/> so that spaces and linefeeds are not repercuted from xml to output file. Nevertheless, I have some problems when I use CDATA expression. Imagine my xml file is : <ROOT_TAG> <DESTINATION_FILENAME>helloWorld.c</DESTINATION_FILENAME> <MY_NAME>pbodu</MY_NAME> </ROOT_TAG> and my xsl transform is : <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="text" indent="no"/> <xsl:strip-space elements=""/> <xsl:variable name='nl'> <xsl:text>&#xA;</xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:result-document href="{/ROOT_TAG/DESTINATION_FILENAME}"> <xsl:text>/* This C source file was automatically generated /</xsl:text> <xsl:value-of select="$nl"/> <xsl:text>#include &lt;stdio.h&gt;</xsl:text> <xsl:value-of select="$nl"/> <xsl:value-of select="$nl"/> <xsl:text>main{</xsl:text> <xsl:value-of select="$nl"/> <xsl:text> printf("Hello world, my name is %s\n","</xsl:text> <xsl:value-of select="/ROOT_TAG/MY_NAME"/> <xsl:text>");</xsl:text> <xsl:value-of select="$nl"/> <xsl:text>}</xsl:text> </xsl:result-document> </xsl:template> </xsl:stylesheet> then i'll get this clean helloWorld.c file : / This C source file was automatically generated / #include <stdio.h> main{ printf("Hello world, my name is %s\n","pbodu"); } although if I use this xsl stylesheet : <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="text" indent="no"/> <xsl:strip-space elements=""/> <xsl:variable name='nl'> <xsl:text>&#xA;</xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:result-document href="{/ROOT_TAG/DESTINATION_FILENAME}"> <xsl:text>/* This C source file was automatically generated /</xsl:text> <xsl:value-of select="$nl"/> <![CDATA[#include <stdio.h>]]> <xsl:value-of select="$nl"/> <xsl:value-of select="$nl"/> <xsl:text>main{</xsl:text> <xsl:value-of select="$nl"/> <xsl:text> printf("Hello world, my name is %s\n","</xsl:text> <xsl:value-of select="/ROOT_TAG/MY_NAME"/> <xsl:text>");</xsl:text> <xsl:value-of select="$nl"/> <xsl:text>}</xsl:text> </xsl:result-document> </xsl:template> </xsl:stylesheet> I'll get this kind of C code / This C source file was automatically generated */ #include <stdio.h> main{ printf("Hello world, my name is %s\n","pbodu"); } where spaces and linefeed obviously come from the xml code (the more linefeeds between tagged code, the more linefeeds in the output. Is there something wrong with the second stylesheet ? Best regards, PBodu


Replies (2)

RE: CDATA and result-document compatibility - Added by Anonymous over 17 years ago

Legacy ID: #4015749 Legacy Poster: Michael Kay (mhkay)

Writing <![CDATA[#include <stdio.h>]]> in the stylesheet is exactly equivalent to writing #include &lt;stdio.h&gt; It's not equivalent to writing <xsl:text>#include &lt;stdio.h&gt;</xsl:text> If you want to stop the adjacent whitespace bleeding in, you still need the xsl:text delimiters. xsl:strip-space, incidentally, only affects whitespace in the source document, not the stylesheet. Whitespace text nodes in the stylesheet are stripped automatically. But whitespace characters adjacent to non-whitespace are always significant, and using CDATA doesn't change this.

RE: CDATA and result-document compatibility - Added by Anonymous over 17 years ago

Legacy ID: #4015797 Legacy Poster: pbodu (pbodu)

mmmmm that helps, thanks ! So my CDATA use pops a raw text element in the xsl tree, and whitespaces and linefeeds around it stick to it. Better use of CDATA, in this case would be : <xsl:text><![CDATA[#include <stdio.h>]]</xsl:text> (I've just tried, it works fine) Thanks again, PBodu

    (1-2/2)

    Please register to reply