首页 > 开发 > Xml > 正文

使用纯HTML的通用数据管理和服务

2020-07-10 12:45:07
字体:
来源:转载
供稿:网友

使用纯HTML的通用数据管理和服务。然而,为了收集数据,你需要一个数据储存库。要避免使用数据库服务器带来的很多问题,你可以在XML中收集这些数据。下面是我们的项目的基本结构:

<user>
<first_name/>
<last_name/>
<mi/>
</user>

我最初将数据限制为first name,last name和middle。这个页面之后的基本思想是用户信息在这个页面中获得。在用户信息需求得到满足以后,流程必须被转到下一个逻辑收集步骤。为了使事情变得简单,我将把用户功能包装到一个ASP类中。

Function Coalesce(vVar, vAlt)
If vVal = "" Or VarType(vVal) = 1 Or VarType(vVal) = 0 Then
Coalesce = vAlt
Else
Coalesce = vVal
End If
End Function

Class CUser
Private m_SQL, m_DOM

Public Property Get DOM()
Set DOM = m_DOM
End Property

Public Sub saveUser()
m_SQL.save "save_user", m_DOM
End Sub

Public Function validate()
m_DOM.loadXML "<root>" & m_SQL.validateUser(m_DOM) & "</root>"
If Not m_DOM.selectSingleNode("//error") Is Nothing Then
validate = False
Else
validate = True
End If
End Function

Private Sub collectData(dom, oCollection)
Dim nItem, node, parent_node, n, sKey
For nItem = 1 To oCollection.Count
sKey = oCollection.Key(nItem)
Set parent_node = dom.selectSingleNode("//" & sKey & "s")
If Not parent_node Is Nothing Then
For n = 1 To oCollection(sKey).Count
Set node = parent_node.selectSingleNode(sKey & _
"[string(.)='" &
oCollection(sKey)(n) & "']")
If node Is Nothing Then
Set node = dom.createNode(1, sKey, "")
Set node = parent_node.appendChild(node)
End If
node.text = Coalesce(oCollection(sKey)(n), "")
Next
Else
Set node = dom.selectSingleNode("//" & sKey)
If Not node Is Nothing Then _
node.text = Coalesce(oCollection(sKey), "")
End If
Next
End Sub

Private Sub Class_Initialize()
Set m_SQL = New CSQL
Set m_DOM = Server.CreateObject("MSXML2.DOMDocument")
m_DOM.async = False
If VarType(Request ("txtUserXML")) = 0 Or Request ("txtUserXML") = "" Then
m_DOM.loadXML Request("txtUserXML")
Else
m_DOM.load "<root>" & Server.MapPath("user.xml") & "</root>"
End If
collectData m_DOM, Request.Form
collectData m_DOM, Request.QueryString
End Sub

Private Sub Class_Terminate()
Set m_SQL = Nothing
Set m_DOM = Nothing
End Sub

End Class

Class CSQL
Private m_DAL, m_Stream

Public Function save(sStoredProc, oDOM)
'adVarChar = 200
m_DAL.RunSP Array(m_DAL.mp("@xml_param", 200, 8000, oDOM.xml))
End Function

Public Function validateUser(oDOM)
Set m_Stream = m_DAL.RunSPReturnStream("validate_user", Array(_
m_DAL.mp("@xml_param", 200, 8000, oDOM.xml)))
validateUser = m_Stream.ReadText(-1)
m_Stream.Close
End Function

Private Sub Class_Initialize()
Set m_DAL = Server.CreateObject("MyPkg.MyDAL")
m_DAL.GetConnection "some connection string"
Set m_Stream = Server.CreateObject("ADODB.Stream")
End Sub

Private Sub Class_Terminate()
Set m_DAL = Nothing
Set m_Stream = Nothing
End Sub

End Class
CSQL类是基于一个数据访问层(m_DAL)组件MyPkg.MyDAL建立起来的。而这个组件则是基于Fitch和Mather DAL组件建立起来的,这两个组件可以从MSDN找到。这样我们就在SQL Server与你的代码建立了桥梁。


当CUser对象初始化之后,它收集Request数据并使用collectData()子函数将收集到的数据放到UserDOM的一个相应的节点中。(代码我不再解释,因为它本身相当容易理解。)在收集了数据之后(或者不收集数据),我们将使用XSL将数据内容转变成布局。

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform
version="1.0">
<xsl:output method="html"/>

<xsl:template match="/">
<xsl:if test="//error">
<font color="red">*Information in red is required<br/></font>
</xsl:if>
<xsl:apply-templates select="//user"/>
</xsl:template>

<xsl:template match="user">
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='first name']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
First Name:
</font>
<input type="text" name="first_name">
<xsl:attribute name="value"><xsl:value-of
select="first_name"/></xsl:attribute>
</input><br/>
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='mi']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
MI:
</font>
<input type="text" name="mi">
<xsl:attribute name="value"><xsl:value-of select="mi"/></xsl:attribute>
</input><br/>
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='last_name']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
Last Name:
</font>
<input type="text" name="last_name">
<xsl:attribute name="value"><xsl:value-of
select="last_name"/></xsl:attribute>
</input><br/>

ver

共2页上一页12下一页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表