Solved

print .xml document with ASP

Posted on 2010-08-29
14
379 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:mcuk_storm
Comment Utility
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
Comment Utility
It is not sorting the output, IN the groups Alphabetically
0
 
LVL 7

Expert Comment

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

Author Comment

by:akbiro
Comment Utility
pName
0
 
LVL 7

Accepted Solution

by:
mcuk_storm earned 500 total points
Comment Utility
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
Comment Utility
That almost got it.  What about this straggler,..see image.  THANKS!!!!!!
sort-alpha2.bmp
0
 

Author Closing Comment

by:akbiro
Comment Utility
Thank you very much...this is terrific...it is working perfectly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now