Transforming AXL Responses with XSLT
Hello Everyone! This is the first post in what I hope will become a great place for exchanging ideas and code. For my first post, I would like to share something that I had difficulty finding out how to do on the internet and in the forums. I have a .NET class that sends an identify request to an ArcIMS server and then transforms the results using an XSLT Stylesheet. I send the AXL request using the .NET Link.
For this code to work, you will need the .NET Link installed on the machine you are using (server and dev machine). Create a new ASP.NET application in Visual Studio.NET. After you project has been created and loaded into VS.NET, add the .NET Link as a reference in your application. You can do this right-clicking on the references directory in the solution explorer and then choosing Add Reference from the resulting menu. If you don't see the .NET Link in the list of .NET components, do a file system search for the ESRI.ArcIMS.SERVER.dll file. Once you have found the location of the file, you can use the browse button in the Add Reference dialog to add the dll to your project.
Now, for the code.
First, these are the namespaces you'll have to import:
Imports ESRI.ArcIMS.Server
Imports System.Xml
Imports System.Text
The imports statements go (of course) at the top of the code behind file. I have written this sample in VB.NET but if there is enough of an outcry or some people are having trouble with a C# conversion, I will be happy to post it in C# also.
Second, declare your variables and initialize the connector. This is all performed (for the sake of ease) in the Page_Load event procedure. The host name and host service have been changed to protect the innocent.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strrequest As New StringBuilder
Dim strresult As New StringBuilder
Dim cn As New ServerConnection
Dim xslt As New Xsl.XslTransform
Dim xml As New XmlDocument
Dim writer As New System.IO.StringWriter
cn.Host = "localhost"
cn.URIPort = 5300
cn.Scheme = Scheme.HTTP
cn.ServiceName = "localservice"
Next, we formulate an AXL Get_Features request and send it to the server. Again, for brevity, I just put in some extents and a layer id. If you know the exact center point of a building and you know the id of your buildings layer, you can use those.
strrequest.Append("<?xml version=""1.0"" encoding=""UTF-8""?>")
strrequest.Append("<ARCXML version=""1.1"">")
strrequest.Append("<REQUEST>")
strrequest.Append("<GET_FEATURES beginrecord=""0"" outputmode=""newxml"" geometry=""false"" envelope=""true"" compact=""true"">")
strrequest.Append("<LAYER id=""0"" />")
strrequest.Append("<SPATIALQUERY subfields=""#ALL#"" where="""">")
strrequest.Append("<SPATIALFILTER relation=""envelope_intersection"">")
strrequest.Append("<ENVELOPE minx=""0"" miny=""0"" maxx=""1"" maxy=""1""/>")
strrequest.Append("</SPATIALFILTER>")
strrequest.Append("</SPATIALQUERY>")
strrequest.Append("</GET_FEATURES>")
strrequest.Append("</REQUEST>")
strrequest.Append("</ARCXML>")
strresult.Append(cn.Send(strrequest.ToString))
Last, we grab the results and transform them using an XSL stylesheet and clean up our object references:
xml.LoadXml(strresult.ToString)
xslt.Load("http://localhost/netconnector/axl.xsl")
xslt.Transform(xml, Nothing, writer, Nothing)
Response.Write(writer.ToString)
strrequest = Nothing
strresult = Nothing
cn = Nothing
xslt = Nothing
xml = Nothing
writer = Nothing
The XSL stylesheet that I used is the following:
<?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="/ARCXML/RESPONSE/FEATURES">
<table>
<xsl:for-each select="FEATURE">
<tr>
<xsl:for-each select="FIELDS/FIELD">
<td>
<font size="2"><xsl:value-of select="@name"/></font>
</td>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="FIELDS/FIELD">
<td>
<font size="2"><xsl:value-of select="@value"/></font>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
If you have any questions or comments concerning the code, please feel free to comment on this post using the comments link at the bottom of this page. Also, if you would like to see any specific samples/news/information, please leave that information also.
Happy Programming!
<< Home