我正在将大约 4000 个包含 HTML 的文本文件转换为 MySQL 数据库条目。似乎一个简单的方法是在 HTML 中添加几行以使它们显示为 XML 文件,然后将 XML XSLT 转换为 MySQL INSERT 语句。(构建 CSV 也可以,但不太理想,恕我直言)。我已经尝试过这样做,但让我的 XSL 玩得好一点运气不佳。
我在 Windoze 机器上,但可以通过 SSH 连接到我的虚拟主机并运行 PHP,也许是 Perl。希望尽可能自动化。我可以构建文件列表并将其轻松输入脚本。
文件名模式:ab12345.html(数字部分为 3-6 位不等)
文件名内容示例——这是整个文件,没有 HTML 页脚/页眉:
<div class="abEntry"><a name="top"><img width="1" height="1" src="images/common/blank.gif"/></a><div id="abEntryTitle"><div id="abEntryTitleText">What does error note "90210 Cannot Do This Thing" mean?</div></div>
<div class="abEntryParagraph">This error means your McWhopper drive is frazzled. Read me the number off the modem--thats the little boxy thing attached to the big boxy thing--thanks.</div>
<div class="abEntryDocumentNumber">ab90210</div>
MySQL 列以及我希望它们如何映射回上面的内容
EntryID = auto increment
title = contents of #abEntryTitleText
content = contents of #abEntryParagraph
lastupdated = curdate
related = "1"
visible = "1"
sortorder = "0"
userid = "1"
views = "0"
posvotes = "1"
negvotes = "0"
score = null
emailed = null
detectrelated = "1"
metakeywords = null
metadescription = contents of #abEntryDocumentNumber
startdate = curdate
enableexpiry = "0"
expirydate = null
featured = "0"
workflowstatus = "auto_approved"
我尝试过的 XSL:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform ">
<xsl:output method="html" indent="no"/>
<xsl:template match="/"><xsl:apply-templates/></xsl:template>
<xsl:template match="content">
<xsl:text>INSERT INTO questions (approved, title, description, publishDate) VALUES </xsl:text><xsl:text>(1, </xsl:text><xsl:value-of select="id(abEntryTitleText)"/><xsl:text>, </xsl:text>
<xsl:copy-of select="node()|@*"/>
<xsl:text>, </xsl:text>TODAY<xsl:text>,1, 1)</xsl:text>
</xsl:template>
</xsl:transform>