Solved

print .xml document with ASP

Posted on 2010-08-29
14
385 Views
Last Modified: 2012-05-10
The attached script, print.asp reads an .xml file and displays it in a format and is printable as an inventory list.  It is supposed to group the output in the form by category.
For some reason, as you can see in the attached screen shot, it is not putting all the records for the category <malawi> into the same group.  It does the same thing with <plants>.  What am I doing wrong>
<table border="1" cellpadding="2" cellspacing="0" style="font-family:arial;font-size:12px">
<tr>
	<th>ShipFish2U.com</th>
		
<%
Dim objXML : Set objXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
objXML.ASync = False
objXML.setProperty "SelectionLanguage","XPath"
If Not objXML.Load(Server.MapPath("/product.xml")) Then
	Response.Write "Error loading XML: " & objXML.parseError.reason
	Response.End
End If

Dim dictCats : Set dictCats = Server.CreateObject("Scripting.Dictionary")

Set objResults = objXML.selectNodes("//row[hide!='true']")
If objResults.length > 0 Then
	Dim objNode, objSubNode, strCurrentCat, intCatCount
	strCurrentCat = ""
	intCatCount = 0
	Do
		Set objNode = objResults.nextNode()
		If objNode Is Nothing Then Exit Do
		Set objSubNode = objNode.selectSingleNode("category")
		If Not objSubNode Is Nothing Then
			If Not strCurrentCat = LCase(objSubNode.text) Then
				strCurrentCat = LCase(objSubNode.text)
				Response.Write "<tr><td style=""font-size:16px;font-weight:bold"" colspan=""20"">&nbsp;</td></tr>"
				Response.Write "<tr><td style=""font-size:16px;font-weight:bold"" colspan=""20"";>" & UCase(strCurrentCat) & "&nbsp;</td></tr>"
				Response.Write "<tr style=""font-weight:bold""align:center""><td colspan=""2"">&nbsp;</td><td colspan=""3"">Large</td><td colspan=""3"">Medium</td><td colspan=""3"">Small</td></tr>"
				Response.Write "<tr style=""font-weight:bold""><td colspan=""1"">Scientific Name</td><td colspan=""1"">Common Name</td><td colspan=""1"">Singles</td><td colspan=""1"">Pairs</td><td colspan=""1"">Trios</td><td colspan=""1"">2 Lot</td><td colspan=""1"">4 Lot</td><td colspan=""1"">6 Lot</td><td colspan=""1"">6 Lot</td><td colspan=""1"">9 Lot</td><td colspan=""1"">12 Lot</td></tr>"
			End If
			Response.Write "<tr>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("pName")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("pSubName")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice3")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice3")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice3")) & "&nbsp;</td>"
			Response.Write "</tr>"
		End If
	Loop
End If

Function getNodeValue(objNode)
	If Not objNode Is Nothing Then
		getNodeValue=objNode.text
	Else
		getNodeValue=""
	End if
End Function

Set objResults = Nothing
Set objXML = Nothing
%>
</tr>
</table>

Open in new window

print-asp-output.bmp
0
Comment
Question by:akbiro
  • 7
  • 7
14 Comments
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33552888
Hi, it would appear that you are missing the XSLT part from the previous solution which is what ordered the XML results by category
0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33552900
The code below should work, you will just need to change the path to product-order.xsl  which is the XSL from from the original question
<table border="1" cellpadding="2" cellspacing="0" style="font-family:arial;font-size:12px">
<tr>
	<th>ShipFish2U.com</th>
		
<%
Dim objXSLT : Set objXSLT = Server.CreateObject("Msxml2.DOMDocument.6.0")
objXSLT.ASync = False
objXSLT.setProperty "SelectionLanguage","XPath"
If Not objXSLT.Load(Server.MapPath("/product-order.xsl")) Then
	Response.Write "Error loading XSLT: " & objXSLT.parseError.reason
	Response.End
End If

Dim objXML : Set objXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
objXML.ASync = False
objXML.setProperty "SelectionLanguage","XPath"
If Not objXML.Load(Server.MapPath("/product.xml")) Then
	Response.Write "Error loading XML: " & objXML.parseError.reason
	Response.End
End If

strSortedXML = objXML.transformNode(objXSLT)
If Not objXML.LoadXML(strSortedXML) Then
	Response.Write "Error loading XML: " & objXML.parseError.reason
	Response.End
End If

Dim dictCats : Set dictCats = Server.CreateObject("Scripting.Dictionary")

Set objResults = objXML.selectNodes("//row[hide!='true']")
If objResults.length > 0 Then
	Dim objNode, objSubNode, strCurrentCat, intCatCount
	strCurrentCat = ""
	intCatCount = 0
	Do
		Set objNode = objResults.nextNode()
		If objNode Is Nothing Then Exit Do
		Set objSubNode = objNode.selectSingleNode("category")
		If Not objSubNode Is Nothing Then
			If Not strCurrentCat = LCase(objSubNode.text) Then
				strCurrentCat = LCase(objSubNode.text)
				Response.Write "<tr><td style=""font-size:16px;font-weight:bold"" colspan=""20"">&nbsp;</td></tr>"
				Response.Write "<tr><td style=""font-size:16px;font-weight:bold"" colspan=""20"";>" & UCase(strCurrentCat) & "&nbsp;</td></tr>"
				Response.Write "<tr style=""font-weight:bold""align:center""><td colspan=""2"">&nbsp;</td><td colspan=""3"">Large</td><td colspan=""3"">Medium</td><td colspan=""3"">Small</td></tr>"
				Response.Write "<tr style=""font-weight:bold""><td colspan=""1"">Scientific Name</td><td colspan=""1"">Common Name</td><td colspan=""1"">Singles</td><td colspan=""1"">Pairs</td><td colspan=""1"">Trios</td><td colspan=""1"">2 Lot</td><td colspan=""1"">4 Lot</td><td colspan=""1"">6 Lot</td><td colspan=""1"">6 Lot</td><td colspan=""1"">9 Lot</td><td colspan=""1"">12 Lot</td></tr>"
			End If
			Response.Write "<tr>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("pName")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("pSubName")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("lPrice3")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("mPrice3")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice1")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice2")) & "&nbsp;</td>"
			Response.Write "<td>" & getNodeValue(objNode.selectSingleNode("sPrice3")) & "&nbsp;</td>"
			Response.Write "</tr>"
		End If
	Loop
End If

Function getNodeValue(objNode)
	If Not objNode Is Nothing Then
		getNodeValue=objNode.text
	Else
		getNodeValue=""
	End if
End Function

Set objResults = Nothing
Set objXML = Nothing
%>
</tr>
</table>

Open in new window

0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33552916
You could also solve this by changing the access -> xml asp code and adding an order by clause to the sql query so it orders it there instead, then you wouldn't need the XSLT sheet.

Change:
  Dim objRecordSet : Set objRecordSet = objConnection.Execute("SELECT * FROM `" & strTableName & "`", objRecordSet)

To:
  Dim objRecordSet : Set objRecordSet = objConnection.Execute("SELECT * FROM `" & strTableName & "` ORDER BY category ASC", objRecordSet)

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:akbiro
ID: 33553302
Again...cool beans...I can't wait to get back to my computer and work on these.  THANKS and THANKS again...I will let you know
0
 

Author Comment

by:akbiro
ID: 33553559
Forgive me. but if I take the first suggestion above, where do I find th xslt code and when I do, I need to name it and somthing like sort.xslt and change the path in the code above to that file?
And, will that sort also alphabetize the records within the group.

Sorry...but this stuff comes slow and hard for me.

Thanks!
0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33553599
The XSLT is the sort.xsl one from the question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_26420449.html#a33495884

If you add that in and change the path that should solve the sorting problem
0
 

Author Comment

by:akbiro
ID: 33553656
It worked PERFECTLY AGAIN...is there a way to add to that sort xslt so that the output is sorted by group and then display in alphabetical order?  Can it be added to the sort script
<?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="/">
		<rows>
			<xsl:for-each select="//row[hide!='true']">
				<xsl:sort select="category"/>
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</rows>
	</xsl:template>
</xsl:stylesheet> 

Open in new window

0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33553677
It should already do that by default, but adding order="ascending" to the xsl:sort element as below may fix it:-
<?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="/">
		<rows>
			<xsl:for-each select="//row[hide!='true']">
				<xsl:sort select="category" order="ascending" />
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</rows>
	</xsl:template>
</xsl:stylesheet> 

Open in new window

0
 

Author Comment

by:akbiro
ID: 33553758
It is not sorting the output, IN the groups Alphabetically
0
 
LVL 7

Expert Comment

by:mcuk_storm
ID: 33553897
do you want it sorted by pSubName or pName within the categories?
0
 

Author Comment

by:akbiro
ID: 33554479
pName
0
 
LVL 7

Accepted Solution

by:
mcuk_storm earned 500 total points
ID: 33554556
This is now adding a second sort condition, so it should sort by category first then by pName, the below code should replace the current xsl file
<?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="/">
		<rows>
			<xsl:for-each select="//row[hide!='true']">
				<xsl:sort select="category" order="ascending" />
				<xsl:sort select="pName" order="ascending" />
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</rows>
	</xsl:template>
</xsl:stylesheet> 

Open in new window

0
 

Author Comment

by:akbiro
ID: 33557275
That almost got it.  What about this straggler,..see image.  THANKS!!!!!!
sort-alpha2.bmp
0
 

Author Closing Comment

by:akbiro
ID: 33611990
Thank you very much...this is terrific...it is working perfectly.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question