How to visually format remote server XML query response?
I'll preface this by stating I am a rank amateur when it comes to web dev.
I have a web appliance that provides XML data when issued a query string such as:
https://example.com/api/reporting.ns?username=name&password=password&generate_report=SupportSession&start_date=2009-04-01&duration=0&limit=all
I created a simple form that allows users to modify values of this query and have the appropriate XML returned. Here is that form:
<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">
What I have not been able to accomplish is formatting that XML response so it looks pretty.
I have created an XSLT that formats the XML nicely and have used javascript to transform the XML using the XSLT like this:
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);
}
}
Which works locally but I run up against the cross domain security problem as the web appliance is hardened and I am not able to place any code on it.
I have been searching this site and the web for a couple days looking for a method to accomplish this using other means [asp?] and have been largely unsuccessful. The main reason this is so is that I don't know asp so I'm not sure how to attack it.
I think creating the query string in a form, then loading the resulting xml response from the web appliance to a string and applying the XSLT that resides on the server to it might be an option but h开发者_StackOverflowaven't been able to make it work; again because I'm not really sure what I'm doing.
Here is my 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>
And a snippet of the XML the web appliance outputs:
<?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&dl_action=chat&view=1&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&dl_action=chat&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>
Further information:
I found a way in asp to retrieve remote xml that works for URL's like http://example.com/file.xml
but doesnt work for my URL. Maybe because it is a query? I've looked at way to encode/encapsulate my URL but haven't found anything that works.
http://asp101.com/samples/xmlxsl_remote.asp
Any help you could provide would be greatly appreciated.
Thanks!
Here is how I accomplished this. It's hacky, terrible code that even I can tell needs optimization but it works. I use one xslt to transform the response to the screen and another to transform the response to 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>
精彩评论