?
Solved

How do I navigate to different areas of my XML document using my .asp web page?

Posted on 2005-04-21
4
Medium Priority
?
161 Views
Last Modified: 2010-04-06
I am using the following .asp page to generate html code from my xml document.

".asp page"
<%@ LANGUAGE = JavaScript %>
<%
  // Set the source and style sheet locations here
  var sourceFile = Server.MapPath("catalog.xml");
  var styleFile = Server.MapPath("catalog.xsl");

  // Load the XML
  var source = Server.CreateObject("Msxml2.DOMDocument");
  source.async = false;
  source.resolveExternals = false
  source.load(sourceFile);

  // Load the XSLT
  var style = Server.CreateObject("Msxml2.DOMDocument");
  style.async = false;
  style.resolveExternals = false
  style.load(styleFile);
  Response.Write(source.transformNode(style));
%>

"XML Document"
<?xml version="1.0" standalone="yes"?>
<Catalog xmlns="/schema">
  <Item>
    <ID>3</ID>
    <Name>Aniseed Syrup</Name>
    <Price>10</Price>
    <Stock>13</Stock>
  </Item>
  <Item>
    <ID>40</ID>
    <Name>Boston Crab Meat</Name>
    <Price>18.4</Price>
    <Stock>123</Stock>
  </Item>
</Catalog>

"XSL document"
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
  <HTML>
    <BODY>
      <TABLE BORDER="2">
        <TR>
          <TD><P ALIGN="center"><B>ID</B></P></TD>
          <TD><P ALIGN="center"><B>Name</B></P></TD>
          <TD><P ALIGN="center"><B>Price</B></P></TD>
          <TD><P ALIGN="center"><B>Stock</B></P></TD>
        </TR>
      <xsl:for-each select="Catalog/Item">
        <TR>
          <TD><xsl:value-of select="ID" /></TD>
          <TD><xsl:value-of select="Name" /></TD>
          <TD><xsl:value-of select="Price" /></TD>
          <TD><xsl:value-of select="Stock" /></TD>
        </TR>
      </xsl:for-each>
      </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

This is just a model, but it works.  What I would like to do is to access different areas of my XML data to create different html pages.  How can I go about it. Is there some way to do it using the url to the .asp file.  e.g http://domain.com/start.asp?ID=3 or something along those lines? Are there other options?
0
Comment
Question by:racing_chicken
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:StuF
ID: 13843536
Hi,

Could you try:

  style.load(styleFile);
  Response.Write(source).selectSingleNode('Catalog/Item[ID=3]').transformNode(style)

or After the source has been loaded then remove all unrequired nodes

source.load(sourceFile);
source.selectNodes['Catalog/Item[ID!=3]'].removeAll()

Not sure if these will work but its worth a go

Have fun
StuF
0
 

Author Comment

by:racing_chicken
ID: 13845159
Thanks Stuf, but I'm looking for a different solution.

When there is more data, I want to be able to generate different html pages on my website, with different data from my XML document,  based solely on what paramenters I add to the url I enter into the browser. I know it can be done, I just don't know how. It may involve xpath or xpointer, or not, I don't really know.

Let's say my XML is team stats.

www.teamstats.com/start.asp?stats&tm1 would show me team 1's stats while
www.teamstats.com/start.asp?stats&tm2 would show me team 2's stats.

the format for the parameters is just a guess.

R.C.
0
 
LVL 1

Accepted Solution

by:
StuF earned 2000 total points
ID: 13857790
OK, two options

first set the URL to be www.teamstats.com/start.asp?stats&tm=1

then change the code to be:

var teamNumber = Request.Item("tm")
source.load(sourceFile);
source.selectNodes['Catalog/Item[ID!=' +teamNumber + ']'].removeAll()

Alternatively use MSXML2.XSLTemplate and pass in a param in to the XSL

First change the xsl to have
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:param name="TeamNumber"/>
....
<xsl:for-each select="Catalog/Item[ID=$TeamNumber]">
...

Now change the asp to have

// Create the XSL Template
var oXSLtemplate = new  Server.CreateObject('MSXML2.XSLTemplate');
// Set the stylesheet to be the DOM
oXSLtemplate.stylesheet = style

// Create the Processor
var teamNumber = Request.Item("tm")
var processor = oXSLtemplate.createProcessor();
processor.addParameter('TeamNumber',teamNumber);
// Set the processor input to be the source DOM
processor.input = source
processor.transform();
var output = processor.output;
Response.Write(output);

Would this be any better

StuF

0
 

Author Comment

by:racing_chicken
ID: 13883341
Thank you StuF,  you set me on the right path.  Along the way I changed the problem a little, but I solved it.  The second solution was the most help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

599 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