2009-06-15 162 views
1

我想下面的SQL Server代碼爲VBScript中傳統的ASP轉換轉換SQL代碼...使用XML/XPath來的VBScript(經典ASP)

DECLARE @idoc int 
DECLARE @xdoc nvarchar(4000) 
DECLARE @xmldoc xml 
Set @xmldoc = '<Root><Authors au_id="409-56-7008" au_lname="Bennet" au_fname="Abraham"><Titles title="The Busy Executive&apos;s Database Guide"/></Authors><Authors au_id="648-92-1872" au_lname="Blotchet-Halls" au_fname="Reginald"><Titles title="Fifty Years in Buckingham Palace Kitchens"/></Authors><Authors au_id="238-95-7766" au_lname="Carson" au_fname="Cheryl"><Titles title="But Is It User Friendly"/></Authors><Authors au_id="722-51-5454" au_lname="DeFrance" au_fname="Michel"><Titles title="The Gourmet Microwave"/></Authors><Authors au_id="712-45-1867" au_lname="del Castillo" au_fname="Innes"><Titles title="Silicon Valley Gastronomic Treats"/></Authors><Authors au_id="427-17-2319" au_lname="Dull" au_fname="Ann"><Titles title="Secrets of Silicon Valley"/></Authors><Authors au_id="267-41-2394" au_lname="Ellis" au_fname="Michael"><Titles title="Cooking with Computers: Surreptitious Balance Sheets"/><Titles title="Sushi for Anyone"/></Authors><Authors au_id="213-46-8915" au_lname="Green" au_fname="Marjorie"><Titles title="The Busy Executive&apos;s Database Guide"/><Titles title="You Can Combat Computer Stress"/></Authors></Root>' 

SELECT 
Authors.value('./@au_id', 'varchar(20)') as au_id, 
Authors.value('./@au_fname', 'varchar(20)') as au_fname, 
Authors.value('./@au_lname', 'varchar(20)') as au_lname, 
Authors.value('Titles[1]/@title', 'varchar(20)') as Title 
FROM 
@xmldoc.nodes('/Root/Authors') as TableValues(Authors) 

如何寫在傳統的ASP(VBScript中的上述代碼)?

你能幫我嗎?

回答

3
<% 

Dim xmldoc 
Set xmldoc = Server.CreateObject("MSXML2.DOMDocument.4.0") 

xmldoc.SetProperty "SelectionLanguage", "XPath" 
''# note the escaped double quotes 
xmldoc.LoadXml "<Root><Authors au_id=""409-56-7008"" au_lname=""Bennet"" au_fname=""Abraham""><Titles title=""The Busy Executive&apos;s Database Guide""/></Authors><Authors au_id=""648-92-1872"" au_lname=""Blotchet-Halls"" au_fname=""Reginald""><Titles title=""Fifty Years in Buckingham Palace Kitchens""/></Authors><Authors au_id=""238-95-7766"" au_lname=""Carson"" au_fname=""Cheryl""><Titles title=""But Is It User Friendly""/></Authors><Authors au_id=""722-51-5454"" au_lname=""DeFrance"" au_fname=""Michel""><Titles title=""The Gourmet Microwave""/></Authors><Authors au_id=""712-45-1867"" au_lname=""del Castillo"" au_fname=""Innes""><Titles title=""Silicon Valley Gastronomic Treats""/></Authors><Authors au_id=""427-17-2319"" au_lname=""Dull"" au_fname=""Ann""><Titles title=""Secrets of Silicon Valley""/></Authors><Authors au_id=""267-41-2394"" au_lname=""Ellis"" au_fname=""Michael""><Titles title=""Cooking with Computers: Surreptitious Balance Sheets""/><Titles title=""Sushi for Anyone""/></Authors><Authors au_id=""213-46-8915"" au_lname=""Green"" au_fname=""Marjorie""><Titles title=""The Busy Executive&apos;s Database Guide""/><Titles title=""You Can Combat Computer Stress""/></Authors></Root>" 

Dim Author 

Response.Write "<table>" & vbNewLine 
For Each Author In xmldoc.SelectNodes("/Root/Authors") 
    Response.Write "<tr>" & vbNewLine 
    WriteTableCell Author.GetAttribute("au_id") 
    WriteTableCell Author.GetAttribute("au_fname") 
    WriteTableCell Author.GetAttribute("au_lname") 
    WriteTableCell Author.SelectSingleNode("Titles[1]/@title") 
    Response.Write "</tr>" & vbNewLine 
Next 
Response.Write "</table>" & vbNewLine 

''# table cell output factored into a Sub, for reuse and cleaner code 
Sub WriteTableCell(xmldata) 
    Dim s 

    If IsObject(xmldata) Then 
    If xmldata Is Nothing Then 
     s = "" 
    Else 
     s = xmldata.text 
    End If 
    Else 
    s = CStr(xmldata) 
    End If 

    Response.Write "<td>" & Server.HTMLEncode(s) & "</td>" & vbNewLine 
End Sub 

%> 

測試工作。

+0

偉大的工作Tomalak! – 2009-06-15 18:19:56

3

我建議將這個邏輯封裝到存儲過程中。然後簡單地從你的經典asp頁面調用這個sproc。您將花費太多時間將此邏輯從SQL Server內置的XML解析功能轉換爲VBScript。 VBScript沒有豐富的功能來解析xml文檔。

使用最好的工具來完成這項工作。如果存在管理制約因素,可能試圖吸引某人的理智。我知道這並不能回答你的具體問題,因爲你可能已經預料到了。

Create Procedure GetAuthors 

AS 
    DECLARE @idoc int , 
      @xdoc nvarchar(4000) , 
      @xmldoc xml 

    SELECT @xmldoc = '' 


    SELECT Authors.value('./@au_id', 'varchar(20)') as au_id, 
    Authors.value('./@au_fname', 'varchar(20)') as au_fname, 
    Authors.value('./@au_lname', 'varchar(20)') as au_lname, 
    Authors.value('Titles[1]/@title', 'varchar(20)') as Title 
    FROM @xmldoc.nodes('/Root/Authors') as TableValues(Authors)