How do I traverse through xml-attributes?

I want to present some xml-attributes as records, but I cant quite figure out how to traverse through the xml-file, that looks like this:
 
<map>
        <positions>
                <position level="1" xPosition="80" yPosition="80" xWidth="120" yHeight="60">Bruxelles</position>      
                <position level="2" xPosition="10" yPosition="60" xWidth="100" yHeight="90">New York</position>
                <position level="3" xPosition="100" yPosition="180" xWidth="200" yHeight="20">Los Angeles</position>
                <position level="4" xPosition="150" yPosition="150" xWidth="120" yHeight="30">Singapore</position>
        </positions>
</map>

I have tried to change a code that was doing the job with a xml-file that was formatted with nodeelements ( see attached file).

<%
dim objXML
set objXML = server.CreateObject("Microsoft.FreeThreadedXMLDOM")

if request.QueryString("delete") <> "" then
objXML.Load(xmlpath)
set remove = _
objXML.childnodes(0).childnodes(1).childnodes(request.QueryString("delete"))
objXML.childnodes(0).childnodes(1).removeChild(remove)
objXML.save(xmlpath)
end if

objXML.Load(xmlpath)
set xmlcontents = objXML.DocumentElement.selectSingleNode("positions")
%>

<html>
<body>
  <table width="180">
    <tr>
      <th width="80">Options</th>
      <th width="20">level</th>
      <th width="20">xPosition</th>
      <th width="20">yPosition</th>
      <th width="20">xWidth</th>
      <th width="20">yHeight</th>
         </tr>

    <%
for i = 0 to xmlcontents.ChildNodes.attributes.length - 1
set xmlcontent = xmlcontents.ChildNodes(i).attributes %>
    <tr>
      <td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.Level %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.xPosition %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.yPosition %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.xWidth %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.yWidth %></td>
     </tr>
    <% next %>
   </table>
  </body>
</html>
<%
for i = 0 to xmlcontents.ChildNodes.length - 1 
set xmlcontent = xmlcontents.ChildNodes.item(i)
%>
    <tr> 
      <td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
      <td><%= xmlcontent.ChildNodes.item(1).text%></td>
      <td><%= xmlcontent.ChildNodes.item(2).text%></td>
      <td><%= xmlcontent.ChildNodes.item(3).text%></td>
      <td><%= xmlcontent.ChildNodes.item(4).text%></td>
      <td><%= xmlcontent.ChildNodes.item(5).text%></td>
    </tr>
    <% next %>

Open in new window

SimonAdrianAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SimonAdrianConnect With a Mentor Author Commented:
Okay, I start to get why it might be a sensible solution, but there is still something wrong.
Now I can se the asp-file, but only the header-content, not the xml-content.
And I guess it takes some css-styling to make it look good.
Here is the result of the asp-file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><body><table width="180"><tr><th width="80">Options</th><th width="20">level</th><th width="20">xPosition</th><th width="20">yPosition</th><th width="20">xWidth</th><th width="20">yHeight</th></tr></table></body></html>
0
 
ellandrdCommented:
you would probably be better off using XSL and doing an XML - XSL transform, saves looping through things, and mucking about...
0
 
ellandrdCommented:
for reference -
http://www.w3schools.com/xsl/xsl_transformation.asp

and this may also be usefull

http://www.asp101.com/articles/michael/editingxml/default.asp

if you need / want additional information, let me know and I'll assist where I can

- ellandrd
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
SimonAdrianAuthor Commented:
Well I prefer this way and want to know how it should be done.
0
 
ellandrdCommented:
http://www.w3schools.com/xsl/xsl_transformation.asp will give you a bit of understanding as to what is going on,

your ASP file content would become -

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xmlpath)

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("path to XSL file you created, or the XSL in a string)

'Transform file
Response.Write(xml.transformNode(xsl))
%>

the XSL file content you should use is in the code window below -


hope that makes sense, but let me know if not, or if there are any problems

- ellandrd

<?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="/">
	<html>
	<body>
		<table width="180">
			<tr>
				<th width="80">Options</th>
				<th width="20">level</th>
				<th width="20">xPosition</th>
				<th width="20">yPosition</th>
				<th width="20">xWidth</th>
				<th width="20">yHeight</th>
			</tr>
				<xsl:apply-templates select="/map/positions" />
		 </table>
		</body>
	</html>
</xsl:template>
 
<xsl:template match="template">
			<tr>
				<td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
				<td><xsl:value-of select="@level" /></td>
				<td><xsl:value-of select="@xPosition" /></td>
				<td><xsl:value-of select="@yPosition" /></td>
				<td><xsl:value-of select="@xWidth" /></td>
				<td><xsl:value-of select="@yWidth" /></td>
			 </tr>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
ellandrdCommented:
apologies - change the line  -

<xsl:template match="template">

to

<xsl:template match="position">

in the XSL example given
0
 
SimonAdrianAuthor Commented:
Problem 1: I cant get it to work. I dont know why. I can see the xml- and xsl-file in the browser, but I dont get anything else than an annoying http 500 error from the asp-file.

This is the total content of the asp-file.

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load("positions.xml")

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("positions.xsl")

Response.Write(xml.transformNode(xsl))
%>

Problem 2:
<td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>

In the xsl-file I've had to exclude the above asp-line, wherefrom its possible to edit and delete every record. I didnt include that part of the code to simplify the question. But editing is the whole purpose. Not presentation.
Originally there was an ID in the xml-file, but I didnt think there was any need to show that in my example. But if I want to edit the exact id through the editcontent-file as its shown in the original code, then what?

0
 
ellandrdCommented:
I have found and fixed the problem you seem to have been having, my bad, i included invalid characters in the XSL. Editing and deleting should be pretty simple using this method. is the level="1" part a unique Identity?

i'll knock together a few files that should make this nice and simple for you if it is, if not, then i can surely come up with an alternate solution ..


- ellandrd
0
 
ellandrdCommented:
updated XSL by the way
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" omit-xml-declaration="yes" standalone="no"/>
<xsl:template match="/">
        <html>
        <body>
                <table width="180">
                        <tr>
                                <th width="80">Options</th>
                                <th width="20">level</th>
                                <th width="20">xPosition</th>
                                <th width="20">yPosition</th>
                                <th width="20">xWidth</th>
                                <th width="20">yHeight</th>
                        </tr>
                                <xsl:apply-templates select="/map/positions" />
                 </table>
                </body>
        </html>
</xsl:template>
 
<xsl:template match="position">
                        <tr>
                                <td><a><xsl:attribute name="href">editcontent.asp?id=<xsl:value-of select="@level"/></xsl:attribute>Edit</a> - <a><xsl:attribute name="href">?delete=<xsl:value-of select="@level"/></xsl:attribute>Delete</a></td>
                                <td><xsl:value-of select="@level" /></td>
                                <td><xsl:value-of select="@xPosition" /></td>
                                <td><xsl:value-of select="@yPosition" /></td>
                                <td><xsl:value-of select="@xWidth" /></td>
                                <td><xsl:value-of select="@yWidth" /></td>
                         </tr>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

0
 
ellandrdCommented:
and heres the ASP, though I suspect that it should be fine, I did make a few small changes
<%
'Load XML
set Objxml = Server.CreateObject("Microsoft.XMLDOM")
Objxml.async = false
Objxml.load(server.MapPath("test.xml")) ''// your XML file here
 
'Load XSL
set Objxsl = Server.CreateObject("Microsoft.XMLDOM")
Objxsl.async = false
Objxsl.load(server.MapPath("test.xsl")) ''// your XSL file here
 
Response.Write Objxsl.transformNode(Oxsl) 
%>

Open in new window

0
 
ellandrdConnect With a Mentor Commented:
in the ASP

Response.Write Objxsl.transformNode(Oxsl) should be Response.Write Objxsl.transformNode(Objxsl)

sorry bout that!
0
 
SimonAdrianAuthor Commented:
By the way level could be an unique identifier, yes. From now on imagine that level is changed to id like this
<map>
        <positions>
                <position id="1" xPosition="80" yPosition="80" xWidth="120" yHeight="60">Bruxelles</position>                <position id="2" xPosition="10" yPosition="60" xWidth="100" yHeight="90">New York</position>
                <position id="3" xPosition="100" yPosition="180" xWidth="200" yHeight="20">Los Angeles</position>
                <position id="4" xPosition="150" yPosition="150" xWidth="120" yHeight="30">Singapore</position>
        </positions>
</map>
0
All Courses

From novice to tech pro — start learning today.