Solved

Displaying XML elements in an ASP page

Posted on 2004-08-03
20
759 Views
Last Modified: 2013-11-19
I have an XML document whose elments i need to display in an ASP page. First i have to check wheather a particular element exists then will display it, the reason being my headers on the page will come from element heading.

I can load the document as (http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21029059.html)

sourceFile = objRS("strXML")  //document is coming from Databasde as it is stored there
styleFile = objRS("strXSL")

set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.loadXML(sourceFile)

set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)

then according to the style sheet i want to display header on the page, and when the header is clicked, i can refresh the page and show then that particular element information on the page.

I have taken the following tag off the XML document:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>

Please click the following to look at the XML doc.
http://www.nrccua.org/testing/test.xml 




0
Comment
Question by:tahirjadoon
  • 10
  • 9
20 Comments
 
LVL 16

Expert Comment

by:golfDoctor
ID: 11709028
0
 

Author Comment

by:tahirjadoon
ID: 11709230
Its giving an error on fourth row:

var objXMLDoc = Server.CreateObject("Microsoft.XMLDOM");
objXMLDoc.async = false;
objXMLDoc.load("test.xml");
var objChildNodes = objXMLDoc.documentElement.childNodes;

Error Type:
Microsoft JScript runtime (0x800A01A8)
Object required
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11709378
Try changing your code to this:

    var objXMLDoc = Server.CreateObject("Microsoft.XMLDOM");
    objXMLDoc.async = false;
    var bOK = objXMLDoc.load("test.xml");

    if (bOK)
        var objChildNodes = objXMLDoc.documentElement.childNodes;
    else
        alert( objXMLDoc.parseError.reason );


It sounds like your XML doc hasn't loaded properly. parseError.reason should tell you why.

Hope this helps
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11709409
Doh, server side !!

Change:

     alert( objXMLDOC.parseError.reason );

to:

    Response.Write (objXMLDOC.parseError.reason);
0
 

Author Comment

by:tahirjadoon
ID: 11709814
I have the following

var objXMLDoc = Server.CreateObject("Microsoft.XMLDOM");

objXMLDoc.async = false;
var blnOK = objXMLDoc.load("test.xml");

var bOK = objXMLDoc.load(Server.MapPath("test.xml"));

if (bOK){
      var objChildNodes = objXMLDoc.documentElement.childNodes;
      
      
      //Response.Write(objChildNodes.length)
      
      for(var i=0; i<objChildNodes.length; i++){
            Response.Write(objChildNodes[i].nodeName + "<BR>")
      }
}
else{
    Response.Write(objXMLDoc.parseError.reason );
}

Result of it is like

DISTRIBUTIONAREA
DISTRIBUTIONAREA
DISTRIBUTIONAREA
DISTRIBUTIONAREA
DISTRIBUTION
DISTRIBUTION
DISTRIBUTION
DISTRIBUTION
SUMMARY
SUMMARY

This is correct elements in my document. Now my element name is like following, why only the in Uppercase being pulled. How can i pull the complete Element Name?

<COUNT>
      <DISTRIBUTIONAREA AREATYPE="County"/>
      <DISTRIBUTIONAREA AREATYPE="EPS"/>
      <DISTRIBUTIONAREA AREATYPE="Radius"/>
      <DISTRIBUTIONAREA AREATYPE="State"/>

      <DISTRIBUTION distType="STATE"></DISTRIBUTION>
                <DISTRIBUTION distType="SCF"></DISTRIBUTION>
                 <DISTRIBUTION distType="ZIP"></DISTRIBUTION>
                 <DISTRIBUTION distType="CNTY"></DISTRIBUTION>

      <SUMMARY sumType="SEX"></SUMMARY>
      <SUMMARY sumType="RACE"></SUMMARY>
</COUNT>

Again my test xml is at:

http://www.nrccua.org/testing/test.xml 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11710428
Your current output IS the element names. The AREATYPE, distType and sumType parts that I assume you are referring to are not part of the element name, they are attributes of the element.

What exactly is it that you want to output ? A written example would be useful.
0
 

Author Comment

by:tahirjadoon
ID: 11710649
Out of these elements, which ones will be on the xml doc, i don't know. This XML doc is being created by our process server and then puts it in the database. On web page i will be pulling the xml doc from the database, then will go through the elments and  

if      <DISTRIBUTION distType="STATE"></DISTRIBUTION> is available, will put a link on the page for it. When the user will click this link, page will refresh and this time the contents of this elemnt will show.

Hope this ansers your question.
0
 

Author Comment

by:tahirjadoon
ID: 11710703
how can i get the element name + the attribute name so when i want to display

<DISTRIBUTION distType="STATE"></DISTRIBUTION> contents, only this element content should display and not

<DISTRIBUTION distType="SCF"></DISTRIBUTION>
<DISTRIBUTION distType="ZIP"></DISTRIBUTION>
<DISTRIBUTION distType="CNTY"></DISTRIBUTION>

Also it is important as i have to make sure that if the element name fetched is either

<DISTRIBUTIONAREA AREATYPE="County"/>
<DISTRIBUTIONAREA AREATYPE="EPS"/>
<DISTRIBUTIONAREA AREATYPE="Radius"/>
<DISTRIBUTIONAREA AREATYPE="State"/>

i shouldnt be putting the links for these.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11710754
First the easy part.  You can use an XPath query to select all child nodes of the root node apart from ones with a AREATYPE attribute.

Something like:

   var objChildNodes = objXMLDoc.selectNodes("/*[not(@AREATYPE)]")

This should select all child nodes of the root element that don't have an attribute named AREATYPE.
0
 

Author Comment

by:tahirjadoon
ID: 11715853
var objChildNodes = objXMLDoc.selectNodes("/*[not(@AREATYPE)]")

This only selects the parent element

var objChildNodes = objXMLDoc.selectNodes("/*[not(@AREATYPE)]"); //objXMLDoc.documentElement.childNodes;
      
      
      //Response.Write(objChildNodes.length)
      
      for(var i=0; i<objChildNodes.length; i++){
            Response.Write(objChildNodes[i].nodeName + "<BR>")
      }

Result is

COUNT


0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11716051
Try:

    var objChildNodes = objXMLDoc.selectNodes("//COUNT/*[not(@AREATYPE)]");
0
 

Author Comment

by:tahirjadoon
ID: 11716055
Also i have XSL, how can i display only one element according to this style sheet. Applying it to the whole document and displaying it simple:

sourceFile = objRS("strXML")  //document is coming from Databasde as it is stored there
styleFile = objRS("strXSL")

set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.loadXML(sourceFile)

set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)

Response.Write(source.transformNode(style))



 
0
 

Author Comment

by:tahirjadoon
ID: 11716155
CARL_TAWN following worked fine for pulling the right elements:

var objChildNodes = objXMLDoc.selectNodes("//COUNT/*[not(@AREATYPE)]");

for(var i=0; i<objChildNodes.length; i++)
Response.Write(objChildNodes[i].nodeName + "<BR>" +objChildNodes[i].xml + "<BR><BR>")
      

Result:

DISTRIBUTION
AL 9 AR 11175 AS 25

DISTRIBUTION
359 1 366 9 640 1288

DISTRIBUTION
35979 1 36601 9 64014 78

DISTRIBUTION
AL-097 9 AR-001 128 AR-005 99

SUMMARY
N/A 38 0.2 Male 6884 41.6 Female 9619 58.2

SUMMARY
American Indian/Alaskan Native 664 4.0 Asian/Asian American/Pacific Islander 312 1.9 Black/African American 3370 20.4 Latino/Hispanic/Chicano 666 4.0 White/Caucasian 8125 49.1 Other 432 2.6 Prefer not to Respond 3947 23.9
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11716312
Well thats good. Can you just restate what else (if anything) you want to do with it next ?
0
 

Author Comment

by:tahirjadoon
ID: 11716459
1. How can i distinguish between the different DISTRIBUTION elements and SUMMARY elements. I have 4 DISTRIBUTION elements thats attribute is different:

<DISTRIBUTION distType="STATE"></DISTRIBUTION>
<DISTRIBUTION distType="SCF"></DISTRIBUTION>
<DISTRIBUTION distType="ZIP"></DISTRIBUTION>
<DISTRIBUTION distType="CNTY"></DISTRIBUTION>

<SUMMARY sumType="RACE"></DISTRIBUTION>
<SUMMARY sumType="SEX"></DISTRIBUTION>

Now some of these elements might not be present in the XML doc. Using the above method i can pull what ever elements are present in the XML doc.

2. I need to put links on the asp page (link will be node name) along with the attribute name. How to get the attribute name here?

3. When the link will be cliked, page will refresh, as i will be putting links again on the page, will also check if node name + attribute name is same as user has clicked before then will display the content of that element. Here is the user had clicked element distribution attribute state, will only display content of this DISTRIBUTION element and not the rest of the DISTRIBUTION elements.

4. I have a style sheet XSL document, how can i display element in 3 according to the style sheet.    

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11716991
You can probably do everything you need using just one XSL stylesheet.  I leave work soon and will have a go at knocking something together as an example.
0
 

Author Comment

by:tahirjadoon
ID: 11717211
Point 1 above:

var strEleName = objChildNodes[i].nodeName;
var strEleAttrib = objChildNodes[i].attributes[0].value;

Now main question is how can i apply my XSL to the above element stored in strEleName with attribute stored in strEleAttrib?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 11718034
Try this to start:

'// Asp page

    <%
        Set oDom = Server.CreateObject("MSXML2.DOMDocument.4.0")
        Set oXSL = Server.CreateObject("MSXML2.DOMDocument.4.0")

        oXSL.setProperty "SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"

        oDom.load Server.MapPath("test.xml")
        oXSL.load Server.MapPath("test.xsl")

        oXSL.selectSingleNode("//xsl:stylesheet/xsl:param[@name='node']").Text = Request.Querystring("node")
        oXSL.selectSingleNode("//xsl:stylesheet/xsl:param[@name='value']").Text = Request.Querystring("value")

        Response.Write oDom.transformNode(oXSL)
    %>

With this XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:param name="node" />
        <xsl:param name="value" />

        <xsl:template match="/">
            <xsl:apply-templates />
        </xsl:template>

        <xsl:template match="*[parent::COUNT]">
            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:text>test.asp?node=</xsl:text>
                    <xsl:value-of select="name()" />
                    <xsl:text>&amp;value=</xsl:text>
                    <xsl:value-of select="@*[1]" />
                </xsl:attribute>
                <xsl:value-of select="name()" />
            </xsl:element>
            <xsl:if test="name()=$node and @*[1]=$value">
                <p>Selected Node</p>
            </xsl:if>
            <br />
        </xsl:template>

    </xsl:stylesheet>


For starters at least. Obviously you would need to modify it slightly to display what info you want.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11718047
P.S.

   That ASP code is VBScript, you will probably need to adapt it for JScript if thats what you want to use.
0
 

Author Comment

by:tahirjadoon
ID: 11763319
thanks a lot carl_tawan, project is complete and i am happy with it. Here is the final complete code that i have used:

var objXMLDoc = Server.CreateObject("Microsoft.XMLDOM");
objXMLDoc.async = false;
var blnOK = objXMLDoc.loadXML(objRS("XMLDoc"));

if(blnOK){
      //We have the xml document and it is loaded
               //Get the child nodes
      var objChildNodes = objXMLDoc.documentElement.childNodes;
                        
      //Only proceed if the child nodes length is greater than 0
                if(objChildNodes.length > 0){
                                //parse through the nodes
            for(var i=0; i<objChildNodes.length; i++){
                  var strElement  = ""+objChildNodes[i].nodeName;
                  var strAttributeValue       = ""+objChildNodes[i].attributes[0].value;
                  var strAttributeName       = ""+objChildNodes[i].attributes[0].name;
                                                //I crated the links out of these 3 variables
                                                cLink = strElement + "_" + strAttributeName + "_" + strAttributeValue;

                                                //This links gets displayed on the page, when the user click this link this same very link info is passed back to the page as QueryString and i check if my QueryString is equal to cLink, i display the content of the node
                                                  if(cLink == Request.QueryString("Link")){
                                                              //Create style Object
                        var objStyle = Server.CreateObject("Microsoft.XMLDOM");
                        objStyle.async = false;
                        objStyle.load(Server.MapPath("CountStyle.xsl"));

                                                                var strType = strElement + "[@" + strAttributeName + "='" + strAttributeValue + "']";
                        var objSelectedNode = objXMLDoc.documentElement.selectSingleNode(strType);

                                                                 Response.Write(objSelectedNode.transformNode(objStyle.documentElement))

                                                   }
                                  }                  
                }
}
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API Soap Calls 4 116
Select2 jquery help 9 89
asp syntax 3 50
How to write an English digest paper 6 50
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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