473,769 Members | 5,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CreateOb ject("ADODB.Rec ordSet")

RS.ActiveConnec tion = "Provider=MSDAO SP; Data
Source=MSXML2.D SOControl.2.6;"

RS.Open(Server. MapPath("portfo lio.xml"))

Count = 0
Do While Not RS.EOF

strApplicationT itle = ""
strApplicationI mageSmall = ""
strApplicationT ext = ""
strApplicationL iveSite = ""
strApplicationD emoSite = ""
strApplicationP rojectSite = ""

strApplicationT itle = RS(0)
strApplicationI mageSmall = RS(1)
strApplicationT ext = RS(2)
strApplicationL iveSite = RS(3)
strApplicationD emoSite = RS(4)
strApplicationP rojectSite = 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 2824
[..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.DOMDocum ent 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("M sxml2.DOMDocume nt") '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.setProp erty "SelectionLangu age", "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.selectN odes(pstrXPath)
For Each pobjNode in pobjNodeList
pstrAppTitle = pobjNode.select SingleNode("tit le").nodeTypedV alue
pstrAppImage = pobjNode.select SingleNode("ima ge").nodeTypedV alue
pstrAppText = pobjNode.select SingleNode("tex t").nodeTypedVa lue
pstrAppLiveSite = pobjNode.select SingleNode("liv esite").nodeTyp edValue
'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>Applicat ion 1</title>
<image>An Image</image>
<text>Some text
<br/>Some more text on a new line
</text>
<livesite>htt p://www.msn.com</livesite>
<demosite>htt p://www.microsoft.c om</demosite>
<projectsite>ht tp://www.topxml.com/Xselerator</projectsite>
</application>
<application>
<title>Applicat ion 2</title>
<image>Anothe r Image</image>
<text>Some text
<br/>Some more text on a new line
</text>
<livesite>htt p://www.msn.com</livesite>
<demosite>htt p://www.microsoft.c om</demosite>
<projectsite>ht tp://www.topxml.com/Xselerator</projectsite>
</application>
</portfolio>

XSLT:
<?xml version="1.0"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" omit-xml-declaration="ye s"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!-- Portfolio -->
<xsl:template name="portfolio " match="portfoli o">
<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="applicati on" match="applicat ion">
<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>Applicati on 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>Applicati on 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********@N O-SPAM.kingswoodw eb.net> wrote in message
news:pg******** ************@ne ws-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("M sxml2.DOMDocume nt") '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.setProp erty "SelectionLangu age", "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.setProp erty "SelectionLangu age", "XPath"
pstrXMLFilePath Name = Server.MapPath( "C:\Portfolio.x ml")
pobjXML.load pstrXMLFilePath Name

and
[To output the variables to the browser]
Response.Write "Applicatio n: " & 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.select SingleNode("tit le").nodeTypedV alue
pstrAppImage = pobjNode.select SingleNode("ima ge").nodeTypedV alue
pstrAppText = pobjNode.select SingleNode("tex t").nodeTypedVa lue
pstrAppLiveSite = pobjNode.select SingleNode("liv esite").nodeTyp edValue
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**********@N OSPAMubht.swest .nhs.uk> wrote in message
news:uG******** ******@tk2msftn gp13.phx.gbl...
"Chris Barber" wrote ...
Set pobjXML = CreateObject("M sxml2.DOMDocume nt") '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.setProp erty "SelectionLangu age", "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
4245
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: ct_connect(): network packet layer: internal net library error: Net-Library operation terminated due to disconnect
5
2416
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
2746
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 called "form1", and I have selects called "PORTA", "PORTB" ... etc...
3
4319
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 geographical locations. I have been completely unsucessful in acheiving this goal so far however. Things I have tried: Create a shortcut to ftp sight via browser then tried to map local drive to
47
5290
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 work. I want to output a bit pattern from base 10 input. All I get is a zero after the input...I've looked over the code but can't see the problem...any ideas? /* display the bit pattern corresponding to a signed decimal integer */
3
3347
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 o=getobject(,"ConsoleApplication2.Program") msgbox o.TestString
1
3961
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 class. The service is returning with the error code: -2147467259. I have not succeeded to find any additional information about the error. The "SoapFault" method seems to return only a bunch of question marks.
1
3139
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
3
1351
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
3068
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 explorer would do in this case. The headers I am getting are: Headers {Content-Disposition: attachment; filename="dynamic_file.mdb" Connection: close Cache-Control: private Content-Type: application/octet-stream
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.