Solved

print .xml document with ASP

Posted on 2010-08-29
14
381 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

23 Experts available now in Live!

Get 1:1 Help Now