javascript - How to visually format remote server XML query response? -
i'll preface stating rank amateur when comes web dev.
i have web appliance provides xml data when issued 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 simple form allows users modify values of query , have appropriate xml returned. here 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 have not been able accomplish formatting xml response looks pretty.
i have created xslt formats xml nicely , have used javascript transform xml using xslt 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 ie if (window.activexobject) { ex=xml.transformnode(xsl); document.getelementbyid("content").innerhtml=ex; } // code 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 run against cross domain security problem web appliance hardened , not able place code on it.
i have been searching site , web couple days looking method accomplish using other means [asp?] , have been largely unsuccessful. main reason don't know asp i'm not sure how attack it.
i think creating query string in form, loading resulting xml response web appliance string , applying xslt resides on server might option haven't been able make work; again because i'm not sure i'm doing.
here 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 snippet of xml 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 way in asp retrieve remote xml works url's http://example.com/file.xml
doesnt work url. maybe because query? i've looked @ way encode/encapsulate url haven't found works.
http://asp101.com/samples/xmlxsl_remote.asp
any provide appreciated.
thanks!
here how accomplished this. it's hacky, terrible code can tell needs optimization works. use 1 xslt transform response screen , transform response csv.
<script language="vb" runat="server"> sub page_load (sender object, e eventargs) end sub ' show report on page routine public sub show(source object, e eventargs) try date.parseexact(start_date.text.tostring(), "yyyy-mm-dd", system.globalization.datetimeformatinfo.invariantinfo) catch p formatexception console.writeline("{0} not in 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 excel csv routine public sub download(source object, e eventargs) try date.parseexact(start_date.text.tostring(), "yyyy-mm-dd", system.globalization.datetimeformatinfo.invariantinfo) catch p formatexception console.writeline("{0} not in 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 object, e eventargs) username.text = "" password.text = "" start_date.text = "" duration.text = "0" end sub ' create popup calendar , associated panel private sub calendar1_selectionchanged(byval sender system.object, _ byval e system.eventargs) handles calendar1.selectionchanged start_date.text = calendar1.selecteddate end sub public sub panel(source object, e 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 string) dim myrequest system.net.webrequest = system.net.webrequest.create(sourcefile) dim myresponse system.net.webresponse = myrequest.getresponse() dim myreader system.xml.xmltextreader = new system.xml.xmltextreader(myresponse.getresponsestream()) dim doc system.xml.xmldocument = new system.xml.xmldocument() doc.load(myreader) getxml = doc end function ' download report excel csv routine function dlxml(sourcefile string) dim myrequest system.net.webrequest = system.net.webrequest.create(sourcefile) dim myresponse system.net.webresponse = myrequest.getresponse() dim myreader system.xml.xmltextreader = new system.xml.xmltextreader(myresponse.getresponsestream()) dim doc system.xml.xmldocument = new system.xml.xmldocument() doc.load(myreader) dim xslt new system.xml.xsl.xslcompiledtransform() 'pass in true 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 start day 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>
Comments
Post a Comment