OK, here's a test of how WYMeditor works, because I'm going to try to copy/paste some code in here. I just had a little foray into my past with XSLT. I had 344 old blog posts (starting year 2000!) to convert from XML to SQL. Nothing better than XSL for the job! Here it is.
NB: I haven't restored images as of this writing.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" encoding="ASCII"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ- </xsl:variable>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz-_</xsl:variable>
<xsl:variable name="allowed_letters">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_ </xsl:variable>
<xsl:template match="/">
<xsl:text> USE `sw-blog-dev`;
</xsl:text>
<xsl:apply-templates select="weblog/entry"/>
</xsl:template>
<xsl:template match="entry">
<xsl:text>INSERT INTO `sw-blog-dev`.`posts` (`author_id`,`created_at`,`modified_at`,`permalink`,`title`,`synd_title`,`summary`,`body_raw`,`extended_raw`,`body`,`extended`,`is_active`,`custom_field_1`,`custom_field_2`,`custom_field_3`,`body_searchable`,`extended_searchable`,`text_filter`,`comment_status`) VALUES
</xsl:text>
<xsl:text> (2,</xsl:text> <!— author_id —>
<xsl:text>'</xsl:text><xsl:apply-templates select="date"/><xsl:text> 12:00:00',</xsl:text> <!— created_at —>
<xsl:text>'</xsl:text><xsl:apply-templates select="date"/><xsl:text> 12:00:00',</xsl:text> <!— modified_at —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="PERMALINK" select="title"/><xsl:text>',</xsl:text><!— permalink —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="XHTML" select="title/text()"/><xsl:text>',</xsl:text><!— title —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="SYND_TITLE" select="content"/><xsl:text>',</xsl:text><!— synd_title —>
<xsl:text>'',</xsl:text><!— summary —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="XHTML" select="content"/><xsl:text>',</xsl:text><!— body_raw —>
<xsl:text>'',</xsl:text><!— extended_raw —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="XHTML" select="content"/><xsl:text>',</xsl:text><!— body —>
<xsl:text>'',</xsl:text><!— extended —>
<xsl:text>1,</xsl:text><!— is_active —>
<xsl:text>'',</xsl:text> <!— custom_field_1 —>
<xsl:text>'',</xsl:text> <!— custom_field_2 —>
<xsl:text>'',</xsl:text> <!— custom_field_3 —>
<xsl:text>'</xsl:text><xsl:apply-templates mode="TEXT_ONLY" select="content"/><xsl:text>',</xsl:text><!— body_searchable —>
<xsl:text>'',</xsl:text><!— extended_searchable —>
<xsl:text>'markdown',</xsl:text><!— text_filter —>
<xsl:text>1);
</xsl:text><!— comment_status —>
</xsl:template>
<!— must remember to backslash all single quotes —>
<xsl:template match="date">
<xsl:value-of select="translate(.,'/','-')" />
</xsl:template>
<xsl:template mode="PERMALINK" match="title">
<xsl:value-of select="substring(
translate(
translate(., translate(., $allowed_letters, ''), ''),
$ucletters,
$lcletters
),
0,42)"/>
</xsl:template>
<xsl:template mode="SYND_TITLE" match="content">
<xsl:call-template name="escapesinglequotes">
<xsl:with-param name="arg1"><xsl:value-of select="normalize-space( substring(.,0,42) )"/></xsl:with-param>
</xsl:call-template>
<xsl:text>...</xsl:text>
</xsl:template>
<xsl:template mode="TEXT_ONLY" match="content">
<xsl:call-template name="escapesinglequotes">
<xsl:with-param name="arg1"><xsl:value-of select="normalize-space(.)"/></xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template mode="XHTML" match="content">
<xsl:apply-templates mode="XHTML"/>
</xsl:template>
<xsl:template mode="XHTML" match="node()|@*">
<xsl:copy>
<xsl:apply-templates mode="XHTML" select="@*"/>
<xsl:apply-templates mode="XHTML"/>
</xsl:copy>
</xsl:template>
<xsl:template mode="XHTML" match="text()">
<xsl:call-template name="escapesinglequotes">
<xsl:with-param name="arg1"><xsl:value-of select="normalize-space(.)"/></xsl:with-param>
</xsl:call-template>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template mode="XHTML" match="@*">
<xsl:attribute name="{name()}">
<xsl:call-template name="escapesinglequotes">
<xsl:with-param name="arg1"><xsl:value-of select="normalize-space(.)"/></xsl:with-param>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<xsl:template name="escapesinglequotes">
<xsl:param name="arg1"/>
<xsl:variable name="apostrophe">'</xsl:variable>
<xsl:choose>
<!— this string has at least on single quote —>
<xsl:when test="contains($arg1, $apostrophe)">
<xsl:if test="string-length(normalize-space(substring-before($arg1, $apostrophe))) > 0"><xsl:value-of select="substring-before($arg1, $apostrophe)" disable-output-escaping="yes"/>\'</xsl:if>
<xsl:call-template name="escapesinglequotes">
<xsl:with-param name="arg1"><xsl:value-of select="substring-after($arg1, $apostrophe)" disable-output-escaping="yes"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<!— no quotes found in string, just print it —>
<xsl:when test="string-length(normalize-space($arg1)) > 0"><xsl:value-of select="normalize-space($arg1)"/></xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Niiiiiiice.

Comments
There are 0 comments on this post. Post yours →
Post a comment
Required fields in bold.