0

我正在将大约 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>
4

2 回答 2

0

我不熟悉 xsl,所以我会使用php 的 DOM来解决这个问题。IIRC它可以解析html而不是正确的xml。

www.phpro.org 上的教程

于 2009-02-20T15:50:35.530 回答
0

您正在寻找从 <div> 元素创建插入语句的 xslt 将是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl">

    <xsl:output method="text" indent="no"/>

    <xsl:template match="div[@class='abEntry']">
INSERT INTO questions (approved, title, content, metadescription, publishDate)
VALUES (1, '<xsl:value-of select="normalize-space(*/div[@id='abEntryTitleText']/text())" />', '<xsl:value-of select="normalize-space(div[@class='abEntryParagraph']/text())" />', '<xsl:value-of select="normalize-space(div[@class='abEntryDocumentNumber']/text())" />', TODAY)
    </xsl:template>

</xsl:stylesheet>

您可以进一步修改它以包含其他常量列值。

之后,您显然需要一个脚本或应用程序来在每个文件上运行 xstl。如果您愿意,我可以在 .Net 中快速编写一些东西,但是如果您有一些其他工具/脚本功能方便,使用它可能会更快。

于 2009-02-25T11:27:29.147 回答