473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Accessing XML doc via ASP

Hi all,

I went on an XML course over a year ago and have finally
got around to having a use for it, but do you think I can
actually remember anything - nope!..

Ok, here's what I want to do - I want to store an XML
file with several elements that contain data for
applications we've written - its going to be used by
people in our team as a portfolio to show people when
discussing our services. They will visit an ASP page
which will read in the XML and display it in my format to
the page.

XML File structure -

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="portfolio.xsl"?>
<portfolio>
<application>
<title></title>
<image></image>
<text>text>
<livesite></livesite>
<demosite></demosite>
<projectsite></projectsite>
</application>
</portfolio>
There will be about 12 or more of the <application>
elements within the <portfolio> root element.

The ASP I had written (or some of to keep it simple) is
below :
<%
Dim RS
Set RS = Server.CreateObject("ADODB.RecordSet")

RS.ActiveConnection = "Provider=MSDAOSP; Data
Source=MSXML2.DSOControl.2.6;"

RS.Open(Server.MapPath("portfolio.xml"))

Count = 0
Do While Not RS.EOF

strApplicationTitle = ""
strApplicationImageSmall = ""
strApplicationText = ""
strApplicationLiveSite = ""
strApplicationDemoSite = ""
strApplicationProjectSite = ""

strApplicationTitle = RS(0)
strApplicationImageSmall = RS(1)
strApplicationText = RS(2)
strApplicationLiveSite = RS(3)
strApplicationDemoSite = RS(4)
strApplicationProjectSite = RS(5)

RS.MoveNext
Loop
%>
Within the loop I also run a couple of checks for the
values in the variables, and obviously display the
information to the page - I've left this out due to the
numerous lines of code / html etc...

I was running this quite successfully, but when I started
entering the 'real' information, where I needed to say
start a new paragraph I had entered :

<br/><br/>

When it iterates through the recordset it assumes
(rightly) that this is the next item it should be reading
in - then errors are generated. I do not know before
hand how many <br/>'s or other tags (list items are
another good example) will be embedded within the <text>
element - but there will be some.

Therefore I need a method of reading through the specific
elements that I require, in this case the following :

<title>
<image>
<text>
<livesite>
<demosite>
<projectsite>

If there are other tags within these I do not need them
read as XML as such, but they still need to operate
correctly when the HTML is produced on the page.

I seem to remember something about the XML DOM being able
to do this but have no recollection of how to use it.

My other thought was that I could change the inner tags
within <text> (ie <br/> ) to [br] and then just use the
replace method before going to the page - this would then
allow the XML to be read into the recordset correctly.

Any help in this matter would be appreciated.

Thanks in advance for any/all help,

Regards

Rob

PS: Using the MS site for posting this news item so I
hope it makes it through ok (first time I've tried
posting this way).

Jul 19 '05 #1
6 2808
[..snip..]

Can anyone help me with this one? I didn't want to ask in the XML group as
I suspected this was more an ASP question than XML.

Cheers for any help.

Rob
Jul 19 '05 #2
You really should do this with a MSXML2.DOMDocument object and then use
NodeLists and Nodes to run through the document:

Dim pobjXML
Dim pobjNode
Dim pobjNodeList
Dim pstrXPath

Dim pstrAppTitle, pstrAppImage, pstrAppText, pstrAppLiveSite

Set pobjXML = CreateObject("Msxml2.DOMDocument") 'Or .DOMDocument.3.0 if
you *know* you have MSXML3.0 installed
'These two are important and should not be forgotten (especially the first
one)
pobjXML.async = False
pobjXML.setProperty "SelectionLanguage", "XPath"

'Get a nodelist (a collection) of node objects (each one is a specific
element)
pstrXPath = "/portfolio/application" 'The first slash represents the root
of the document and could be removed in this case
Set pobjNodeList = pobjXML.selectNodes(pstrXPath)
For Each pobjNode in pobjNodeList
pstrAppTitle = pobjNode.selectSingleNode("title").nodeTypedValue
pstrAppImage = pobjNode.selectSingleNode("image").nodeTypedValue
pstrAppText = pobjNode.selectSingleNode("text").nodeTypedValue
pstrAppLiveSite = pobjNode.selectSingleNode("livesite").nodeTypedVal ue
'etc.

'Do something with the variables now if you want such as writing to the
page or running an XSLT on the XML to generate HTML.
'You could in fact negate all of this code with a single XSLT transform
to HTML content.
Next
Set pobjNode = Nothing
Set pobjNodeList = Nothing
Set pobjXML = Nothing

As stated, you could get rid of all this looping and just use a single XSLT
transform to generate HTML such as a table or even the entire page - XSLT is
the power behind XML and is *very* quick at this sort of thing.

Here's a sample XML and XSLT:

XML:
<?xml version="1.0"?>
<portfolio>
<application>
<title>Application 1</title>
<image>An Image</image>
<text>Some text
<br/>Some more text on a new line
</text>
<livesite>http://www.msn.com</livesite>
<demosite>http://www.microsoft.com</demosite>
<projectsite>http://www.topxml.com/Xselerator</projectsite>
</application>
<application>
<title>Application 2</title>
<image>Another Image</image>
<text>Some text
<br/>Some more text on a new line
</text>
<livesite>http://www.msn.com</livesite>
<demosite>http://www.microsoft.com</demosite>
<projectsite>http://www.topxml.com/Xselerator</projectsite>
</application>
</portfolio>

XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!-- Portfolio -->
<xsl:template name="portfolio" match="portfolio">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<th style="text-align: left">Portfolio</th>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<!-- Application -->
<xsl:template name="application" match="application">
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<td>Title: </td>
<td>
<xsl:value-of select="title"/>
</td>
</tr>
<tr>
<td>Image: </td>
<td>
<xsl:value-of select="image"/>
</td>
</tr>
<tr>
<td>Text: </td>
<td>
<!-- Use this form to output the text with the br elements intact -->
<!-- The default template will automatically output the text()
elements -->
<xsl:apply-templates select="text"/>
</td>
</tr>
</table>
</td>
</tr>
</xsl:template>
<!-- Output a br for a br overriding the default template-->
<xsl:template match="br">
<br/>
</xsl:template>
</xsl:stylesheet>

Producing:
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<th style="text-align: left">Portfolio</th>
</tr>
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<td>Title: </td>
<td>Application 1</td>
</tr>
<tr>
<td>Image: </td>
<td>An Image</td>
</tr>
<tr>
<td>Text: </td>
<td>Some text
Some more text on a new line
</td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<td>Title: </td>
<td>Application 2</td>
</tr>
<tr>
<td>Image: </td>
<td>Another Image</td>
</tr>
<tr>
<td>Text: </td>
<td>Some text
Some more text on a new line
</td>
</tr>
</table></td>
</tr>
</table>

NB: Even with 4 portfolios of 12 applications each, this simple transform
took 4.214ms to parse the XML, 0.5ms to parse the XSL and 0.856ms to do the
transform for a total processing time of about 5ms - now that's quick!

Apologies for any line wrapping or typos - I have tested the XSLT in
Xselerator though.

Regards,

Chris.

"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:pg********************@news-text.cableinet.net...
[..snip..]

Can anyone help me with this one? I didn't want to ask in the XML group as
I suspected this was more an ASP question than XML.

Cheers for any help.

Rob

Jul 19 '05 #3
"Chris Barber" wrote ...
As stated, you could get rid of all this looping and just use a single XSLT transform to generate HTML such as a table or even the entire page - XSLT is the power behind XML and is *very* quick at this sort of thing.


Hi Chris - *many* thanks for the code and info - I will try it out now.

I think I am unaware perhaps of the correct use, ie I have already put a
basic xml file together with a basic xslt - which works fine when I browse
the xml page in the browser, however I was unsure of how to get that into an
html page or an asp page - I read somewhere recently that xml was not a
replacement for html - therefore I should probably not be pointing my users
to somepage.xml - instead they should still go to somepage.html or
somepage.asp etc...

When I used the xslt it seemed quite straight forward, and I was able to
loop through and display the information to the page, however I was unable
to work out how to have the items in alternating columns, ie, the first
application on the left of the first table row, and the second application
on the right of the first table row, and then so on - this is why I tried it
in ASP because I knew I'd be able to add some kind of counter to help with
the formatting.

I am also having problems with the whole 'data in xml' - for example - if
xml is such a great way of shipping data back and forth, which is what I
have come to believe, how do people get around the problem with the & in
it?! I work for the NHS (uk) and a typical example might be someones
telephone record, they work in A&E as a good example - the only way I have
found to get around this problem was to create an entity within the xslt
file - BUT - if I do what I want to do through ASP - then am I still able to
use the XSLT file for getting around the ampersand problem?? Incidently, I
only got around this in the case of &nbsp as an entity, I've still not
figured out how to do the same for just the & in text etc....

Its starting to really confuse me....

If I use XML through ASP - can I still use an XSLT to transform the database
its read through anything in my ASP page? If not - then presumably I can
only use XML and XSLT's together, inwhich case I'd lose the functionality
from the ASP - ie, making the data items appear in different columns (in
this example).

I'm going to try and use your code now and see if I can understand any of
it, but if you could offer any more help with the above items I would be
very grateful.

My colleague and I have just returned from a ASP.net course, so I'd like to
get to grips with the XML stuff now before trying to use it within .net...

Thanks in advance,

Rob
Jul 19 '05 #4
"Chris Barber" wrote ...
Set pobjXML = CreateObject("Msxml2.DOMDocument") 'Or .DOMDocument.3.0 if
you *know* you have MSXML3.0 installed
'These two are important and should not be forgotten (especially the first
one)
pobjXML.async = False
pobjXML.setProperty "SelectionLanguage", "XPath"


Do I not need to point the code to the actual XML file at some point here
too Chris? I've run the code thus far, no errors, but no output....?

Regards

Rob
Jul 19 '05 #5
There's no output because I forgot to put the code in to load the XML
document or output the variables to the browser. You need to add :

[To load the XML document]
[Existing line] pobjXML.setProperty "SelectionLanguage", "XPath"
pstrXMLFilePathName = Server.MapPath("C:\Portfolio.xml")
pobjXML.load pstrXMLFilePathName

and
[To output the variables to the browser]
Response.Write "Application: " & pstrAppTitle

in the middle of the looping bit - I didn't do this because I had no idea
what sort of output you wanted.

Try:

For Each pobjNode in pobjNodeList
Response.Write "<p>"
pstrAppTitle = pobjNode.selectSingleNode("title").nodeTypedValue
pstrAppImage = pobjNode.selectSingleNode("image").nodeTypedValue
pstrAppText = pobjNode.selectSingleNode("text").nodeTypedValue
pstrAppLiveSite = pobjNode.selectSingleNode("livesite").nodeTypedVal ue
Response.Write "AppName: " & pstrAppTitle & "<br/>"
Response.Write "AppImage: " & pstrAppImage
Response.Write "</p>"
Next

In terms of XML and flexibility, XML is just a format for the data and XSLT
is just a mechanism for transforming the XML to something else (eg. more XML
or HTML etc.).
There are mechanisms for doing alternating table columns but XSLT is a
little strange at first because it's so alien to other technologies. To use
XSLT then you can do the transform in VBScript at the server and then just
write the resultant HTML to the browser:

ASP Page (Server Side Script)
1. Load XML document
2. Load XSL document
3. Transform the XML with XSL to a variable (containing HTML now)
4. Response.Write the variable to the browser

See: http://xmlfiles.com/xsl/xsl_server.asp for more info on this.

Regards,

Chris.

"Rob Meade" <ro**********@NOSPAMubht.swest.nhs.uk> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
"Chris Barber" wrote ...
Set pobjXML = CreateObject("Msxml2.DOMDocument") 'Or .DOMDocument.3.0 if
you *know* you have MSXML3.0 installed
'These two are important and should not be forgotten (especially the first
one)
pobjXML.async = False
pobjXML.setProperty "SelectionLanguage", "XPath"


Do I not need to point the code to the actual XML file at some point here
too Chris? I've run the code thus far, no errors, but no output....?

Regards

Rob

Jul 19 '05 #6
"Chris Barber" wrote ...
There's no output because I forgot to put the code in to load the XML
document or output the variables to the browser.
hehe - no problem, I did a little searching on line using some of your code
as search criteria and managed to find the load code - got it working :)
in the middle of the looping bit - I didn't do this because I had no idea
what sort of output you wanted.
Yeah sorry, I left my initial post rather sparse on the formatting because
if I posted it all in you'd be scrolling for ages...I managed to get it on
the page in the end.
There are mechanisms for doing alternating table columns but XSLT is a
little strange at first because it's so alien to other technologies. To use XSLT then you can do the transform in VBScript at the server and then just
write the resultant HTML to the browser:

ASP Page (Server Side Script)
1. Load XML document
2. Load XSL document
3. Transform the XML with XSL to a variable (containing HTML now)
4. Response.Write the variable to the browser

See: http://xmlfiles.com/xsl/xsl_server.asp for more info on this.


Thanks for all of your help Chris, things are somewhat clearer now. I will
probably be back again sometime soon to ask more questions, until then -
many thanks.

Regards

Rob

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amy Tseng | last post by:
Hi, I am having a problem accessing SQL Server 2000 via UNIX. I am accessing SQL Server 2000 from Solaris using Sybase Open Client (CT-Lib). Here is the error message: CT-LIBRARY error:...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
3
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set...
1
by: Eirik Brattbakk | last post by:
Hi I have some problems accessing a soap service made in c# using an ATL/MFC client over SSL. I have tried both CSoapMSXMLInetClient and CSoapWininetClient as template arguments with my stub...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
3
by: niju | last post by:
Hi there, I have three web pages (A,B,C). I need to prevent users accessing page B and C without accessing A. What would be the best way to achieve this rule? Many Thanks Niju
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.