2011-02-09 73 views
0

我將在前言中說明我是一名業餘愛好者,當談到web dev。如何以可視方式格式化遠程服務器XML查詢響應?

我有一個Web應用發出的查詢字符串,如時提供XML數據:

https://example.com/api/reporting.ns?username=name&password=password&generate_report=SupportSession&start_date=2009-04-01&duration=0&limit=all 

我創建了一個簡單的形式,它允許用戶修改此查詢的值,並有相應的XML返回。這裏是一個形式:

<form id= "report" action="https://example.com/api/reporting.ns?" name="report"> 
Username: <input name="username"><br /> 
Password: <input type="password" name="password"><br /> 
<input type="hidden" name="generate_report" value="SupportSession"> 
Start Date: <input name="start_date"> 
<input type="hidden" name="duration" value="0"> 
<input type="hidden" name="limit" value="all"> 
<input type="submit" value="Show Report"> 

我一直沒能做到的是格式化該XML響應,所以它看起來很漂亮。

我已經創建了一個很好的格式化XML的XSLT,並使用JavaScript來變換使用這樣的XSLT的XML:

function loadXMLDoc(dname) 
{ 
if (window.XMLHttpRequest) 
    { 
    xhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xhttp.open("GET",dname,false); 
xhttp.send(""); 
return xhttp.responseXML; 
} 

function displayResult() 
{ 
xml=loadXMLDoc("Report.xml"); 
xsl=loadXMLDoc("Report.xsl"); 
// code for IE 
if (window.ActiveXObject) 
    { 
    ex=xml.transformNode(xsl); 
    document.getElementById("content").innerHTML=ex; 
    } 
// code for Mozilla, Firefox, Opera, etc. 
else if (document.implementation && document.implementation.createDocument) 
    { 
    xsltProcessor=new XSLTProcessor(); 
    xsltProcessor.importStylesheet(xsl); 
    resultDocument = xsltProcessor.transformToFragment(xml,document); 
    document.getElementById("content").appendChild(resultDocument); 
    } 
} 

是本地工作,但我跑起來反對跨域安全問題,因爲網絡設備硬化,我無法在其上放置任何代碼。

我一直在尋找這個網站和Web幾天尋找做到這一點使用其他手段的方法[ASP?],並已基本上是不成功的。這主要是因爲我不知道asp,所以我不知道如何去攻擊它。

我覺得在一個形式創建查詢字符串,然後加載從網絡設備到字符串生成的XML響應和應用駐留在服務器它的XSLT可能是一個選項,但一直沒能進行這行得通;因爲我不確定自己在做什麼。

這裏是我的XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<div class="contentBox"> --> 
    <h2>Sessions</h2> 
    <table class="wide grid padding"> 
     <thead> 
      <tr> 
     <th>Session ID</th> 
     <th>Started</th> 
     <th>Duration</th> 
     <th>Public Site</th> 
     <th>CTS Ticket Number</th> 
     <th>Customer's Name</th> 
     <th>Customer's Operating System</th> 
     <th>Representative's Name</th> 
     <th>Chat Transcript Download</th>   
      </tr> 
     </thead> 

     <xsl:for-each select="session_list/session"> 
     <xsl:if test="customer_list/customer/os='Windows® (x86) Click-To-Chat'"> 
     <tr> 
    <td><xsl:value-of select="@lsid"/></td> 
    <td><xsl:value-of select="start_time"/></td> 
    <td><xsl:value-of select="duration"/></td> 
    <td><xsl:value-of select="public_site"/></td> 
    <td><xsl:value-of select="external_key"/></td> 
    <td><xsl:value-of select="primary_customer"/></td> 
    <td><xsl:value-of select="customer_list/customer/os"/></td> 
    <td><xsl:value-of select="primary_rep"/></td> 
    <td><a target="_blank" href="{session_chat_download_url}">Download Chat</a></td>   
     </tr> 
     </xsl:if> 
     </xsl:for-each> 
    </table> 
</xsl:template> 
</xsl:stylesheet> 

和XML的片段在網絡設備輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<session_list xmlns="http://www.networkstreaming.com/namespaces/API" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<session lsid="fef672741e025ffda1acb3041f09252d"> 
<session_type>support</session_type> 
<lseq>2899</lseq> 
<start_time timestamp="1290027608">2010-11-17T16:00:08-05:00</start_time> 
<end_time timestamp="1290027616">2010-11-17T16:00:16-05:00</end_time> 
<duration>00:00:08</duration> 

<public_site id="1">Default</public_site> 
<external_key></external_key> 
<session_chat_view_url>https://mysite.com/session_download.ns?lsid=l%3Dfef672741e025ffda1acb3041f09252d%3Bh%3D9bd6081f0b7fee08dcc32a58ef4cb54c7a0e233d%3Bt%3Dsd%3Bm%3Dchat&amp;dl_action=chat&amp;view=1&amp;sessionType=sd</session_chat_view_url> 
<session_chat_download_url>https://mysite.com/session_download.ns?lsid=l%3Dfef672741e025ffda1acb3041f09252d%3Bh%3D9bd6081f0b7fee08dcc32a58ef4cb54c7a0e233d%3Bt%3Dsd%3Bm%3Dchat&amp;dl_action=chat&amp;sessionType=sd</session_chat_download_url> 
<file_transfer_count>0</file_transfer_count> 

<primary_customer gsnumber="3">Smith, John</primary_customer> 
<customer_list> 
<customer gsnumber="3"> 
<username>Smith, John</username> 
<public_ip>xxx.xxx.xxx.xxx</public_ip> 
<private_ip>xxx.xxx.xxx.xxx</private_ip> 
<hostname>DESKTOP</hostname> 

<os>Windows 7 Enterprise x64 Edition (Build 7600)</os> 
<primary_cust>1</primary_cust> 
<info> 
    <name></name> 
    <company></company> 
    <company_code></company_code> 
    <issue></issue> 
    <details></details> 

</info> 

更多信息:

我在asp中找到了一種方法來檢索遠程xml,它適用於像http://example.com/file.xml這樣的URL,但不適用於我的URL。也許是因爲它是一個查詢?我查看了編碼/封裝我的URL的方式,但沒有找到任何可行的方法。

http://asp101.com/samples/xmlxsl_remote.asp

任何幫助,您可以提供將不勝感激。

謝謝!

回答

1

下面是我如何做到這一點。這是hacky,可怕的代碼,即使我可以告訴需要優化,但它的工作原理。我使用一個xslt將響應轉換爲屏幕,另一個將響應轉換爲csv。

<script language="VB" runat="server"> 
Sub Page_Load (sender As Object, e As EventArgs) 
End Sub 

' Show report on page routine 
Public Sub show(Source As Object, e As EventArgs) 
Try 
Date.ParseExact(start_date.Text.ToString(), "yyyy-mm-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo) 
Catch p As FormatException 
    Console.WriteLine("{0} is not in the correct format.", start_date.Text.ToString()) 
End Try 

Dim url 
url = "https://mysite.com/api/reporting.ns?" & "username=" & username.Text & "&password=" & password.Text & "&generate_report=SupportSession" & "&start_date=" & start_date.Text & "&duration=" & duration.Text & "&limit=all" 
myXml.Document = getXML(url) 
End sub 

' Download report to excel csv routine 
Public Sub download(Source As Object, e As EventArgs) 
Try 
Date.ParseExact(start_date.Text.ToString(), "yyyy-mm-dd",  System.Globalization.DateTimeFormatInfo.InvariantInfo) 
Catch p As FormatException 
    Console.WriteLine("{0} is not in the correct format.", start_date.Text.ToString()) 
End Try 

Dim url 
url = "https://mysite.com/api/reporting.ns?" & "username=" & username.Text & "&password=" & password.Text & "&generate_report=SupportSession" & "&start_date=" & start_date.Text & "&duration=" & duration.Text & "&limit=all" 
csv.Document = dlXML(url) 
End sub 

' Reset form 
Public Sub reset(Source As Object, e As EventArgs) 
username.Text = "" 
password.Text = "" 
start_date.Text = "" 
duration.Text = "0" 
End sub 

' Create Popup Calendar and associated Panel 
Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, _ 
ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged 
Start_date.Text = Calendar1.SelectedDate 
End Sub 

Public Sub panel(Source As Object, e As EventArgs) 
panel1.Visible = Not panel1.Visible 
If panel1.Visible = "True" 
selDate.Text = "Close" 
Else 
selDate.Text = "Select Date" 
End If 
End Sub 

' Show report on page routine 
Function getXML(sourceFile As String) 
Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(sourceFile) 
Dim myResponse As System.Net.WebResponse = myRequest.GetResponse() 
Dim myReader As System.Xml.XmlTextReader = new System.Xml.XmlTextReader(myResponse.GetResponseStream()) 
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument() 
doc.Load(myReader) 

getXML = doc 
End Function 

' Download report to excel csv routine 
Function dlXML(sourceFile As String) 

Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(sourceFile) 
Dim myResponse As System.Net.WebResponse = myRequest.GetResponse() 
Dim myReader As System.Xml.XmlTextReader = new System.Xml.XmlTextReader(myResponse.GetResponseStream()) 
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument() 

doc.Load(myReader) 

Dim xslt As New System.Xml.Xsl.XslCompiledTransform() 'Pass in true to enable XSLT Debugging 
xslt.Load(Server.MapPath("csv.xslt")) 

xslt.Transform(new System.Xml.XmlNodeReader(doc), New System.Xml.XmlTextWriter(Response.Output)) 

Response.ContentType = "text/csv" 
Response.AppendHeader("Content-Disposition", "attachment; filename=Report.csv") 
Response.End() 

End Function 

</script> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<link rel="stylesheet" type="text/css" href="screen.css"/> 
<title>MySite</title> 
</head> 

<body> 

<div id="container" style="width: 86em;"> 
<div id="header" class="contentBox" style="padding-top: 5px; padding-bottom: 30px; border-bottom-width: 2px;"> 
<div ;="" style="margin: 0px 0pt; height: 100px;"> 
<span style="height: 80px; float: left;"> 
    <a target="_blank" href="https://mysite.com"> 
<img alt="Logo" src="Logo.png" id="logo"> 
    </a> 
</span> 
<div style="text-align: right; line-height: 1.5em; padding-top: 35px;" class="pageTitle">MySite</div> 
</div> 
</div> 
<div id="sessionKeyBox" class="contentBox"> 
<h1 class="sectionTitle">Complete Form</h1> 
<form id= "lc" method ="post" runat="server"> 
<div id="form"> 
<table> 
<tr><td align="right"><asp:Label AssociatedControlId="start_date" Text="Start Date:" runat="server" /></td> 
<td align="right"><asp:TextBox id="start_date" runat="server"/></td><td><asp:LinkButton id="selDate" onClick="panel" Text="Select Date" runat="server" /></td></tr> 
<tr><td align="right"><asp:Label AssociatedControlId="duration" Text="Days:" runat="server" /></td> 
<td align="right"><asp:TextBox id="duration" runat="server"/></td><td style="font-size:80%">Use 0 days for start day to present</td></tr> 
<tr><td align="right"><asp:Label AssociatedControlId="username" Text="Username:" runat="server" /></td> 
<td align="right"><asp:TextBox id="username" runat="server"/></td></tr> 
<tr><td align="right"><asp:Label AssociatedControlId="password" Text="Password:" runat="server" /></td> 
<td align="right"><asp:TextBox id="password" TextMode="password" runat="server"/></td></tr> 
<tr><td /><td align="right"><asp:Button OnClick="show" Text="Show Report" runat="server" /> <asp:Button Onclick="reset" Text="Reset" runat="server" /> </td></tr> 
<tr><td></td><td align="right"><asp:Button OnClick="download" Text="Download Report" runat="server" /></td></tr> 
</table> 
<asp:Panel id="Panel1" runat="Server" CssClass="toggleCal" visible="false" HorizontalAlign="center"> 
<asp:Calendar ID="Calendar1" runat="server" /><asp:LinkButton onClick="panel" Text="Close" runat="server" /> 
</asp:Panel> 
</div> 
</div> 
</div> 
</form> 

<asp:Xml id="myXml" transformsource="screen.xsl" runat="server" /> 
<asp:Xml id="csv" runat="server" /> 

</body> 
</html> 
相關問題