Accessing XML doc via ASP

R

Rob Meade

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).
 
R

Rob Meade

[..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
 
C

Chris Barber

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").nodeTypedValue
'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:eek:utput 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.

[..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
 
R

Rob Meade

...
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
 
R

Rob Meade

...
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
 
C

Chris Barber

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").nodeTypedValue
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.

...
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
 
R

Rob Meade

...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top