首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将静态文件转换为MySQL条目

将静态文件转换为MySQL条目
EN

Stack Overflow用户
提问于 2009-02-20 15:37:05
回答 2查看 534关注 0票数 0

我正在转换大约4000个包含超文本标记语言的文本文件为MySQL数据库条目。要做到这一点似乎是一种简单的方法,那就是在超文本标记语言中添加几行代码,使其显示为MySQL文件,然后将其XSLT转换为XSLT INSERT语句。(构建CSV也是可行的,但IMHO不太理想)。我尝试过这样做,但我的XSL运行得很好,但我运气不佳。

我在Windoze机器上,但可以通过SSH连接到我的网络主机并运行PHP,也许是Perl。会喜欢尽可能多地自动化这一点。我可以构建一个文件列表,并轻松地将其输入到脚本中。

文件名模式: ab12345.html (数字部分3-6位)

文件名内容示例--这是整个文件,没有HTML页脚/页眉:

代码语言:javascript
复制
<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列以及我希望它们如何映射回上面的内容

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-02-25 11:27:29

要从元素创建insert语句的xslt应该是

代码语言:javascript
复制
<?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快速编写一些东西,但如果你手头有其他工具/脚本功能,使用起来可能会更快。

票数 0
EN

Stack Overflow用户

发布于 2009-02-20 15:50:36

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

www.phpro.org的教程

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/570020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档