xsl-list
[Top] [All Lists]

[xsl] <xsl:copy-of select="." /> removes (or converts) line breaks in text of child nodes.

2008-05-15 08:38:20
Hi,

My problem is that my new xml-files produced by XSL removes the line breaks from the text in my <field name="content">....</field> nodes when I use a <xsl:copy-of select="." />. The thing is, my original xml file is produced by mysqldump. It looks fine but in <field name="content">....</field> the exported text has line breaks characters at the end of each line. These are marked ^M in vim btw, and is some kind of extra line break that mysql requires. It looks something like this:

-----
                <field name="content">{* DEFAULT FORM LAYOUT / pure CSS *}^M
^M
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; width=&quot;100%&quot; border=&quot;0&quot;&gt;^M
&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;^M
^M
&lt;p&gt;^M
[..]
-----

Now when I XSL transform this file to new files I use <xsl:copy-of select="." /> to export the whole structure as a couple of nodes above the field nodes. Everything exports fine except this text that is slightly changed. The special line breaks have been converted into empty lines. The output looks like this:

-----
                          <field name="value">{* DEFAULT FORM LAYOUT / pure CSS 
*}



&lt;table cellspacing="0" cellpadding="0" width="100%" border="0"&gt;

&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;



&lt;p&gt;
-----

I need a way to retain the mystic ^M characters because without these I won't get line break when I import the xml in mysql.




The structure of the original xml-file looks like this:

-----
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
<database name="sssb">
        <table_structure name="cms_content">
<field Field="content_id" Type="int(11)" Null="NO" Key="PRI" Default="" Extra="" />
                [..]
        </table_structure>
        <table_data name="cms_content">
        <row>
                <field name="content_id">779</field>
                <field name="content_name">AnmÃf¤l skadedjur</field>
                [..]
        </row>
        <row>
                <field name="content_id">780</field>
                [..]
        </row>
        </table_data>
        <table_structure name="cms_content_props">
                <field Field="content_id" Type="int(11)" Null="YES" Key="MUL" 
Extra="" />
                [..]
        </table_structure>
        <table_data name="cms_content_props">
        <row>
                <field name="content_id">780</field>
                [..]
        </row>
        </table_data>
        [..]
</database>
</mysqldump>
-----
I used "[..]" signs to note that I cut the file on these places.


My XSL-file looks like this:

-----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        version="2.0">

<xsl:output method="text" />
<xsl:output method="xml" indent="yes" name="xml" />

<xsl:template match="/">

<!-- If node is table_structure copy it to new file -->
<xsl:for-each select="//table_structure">
        <xsl:variable name="filename" select="concat('tables/',@name,'.xml')" />
        <xsl:value-of select="$filename" />  <!-- Creating  -->
        <xsl:result-document href="{$filename}" format="xml">
                <mysqldump 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
                        <database name="sssb">

                                <xsl:copy-of select="." />

                                <!-- Pick one table_data node after -->
                                <xsl:copy-of 
select="following-sibling::table_data[1]" />
                        </database>
                </mysqldump>
        </xsl:result-document>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>
-----

I use Saxon 9.0.0.4N to XSL transform. Thanks for reading!


--
John Ericson, Fast2
Web: http://www.fast2.se

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--