Solved

print .xml document with ASP

Posted on 2010-08-29
14
384 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now