Solved

Displaying XML elements in an ASP page

Posted on 2004-08-03
20
756 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
Comment Utility
0
 

Author Comment

by:tahirjadoon
Comment Utility
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
Comment Utility
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
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Doh, server side !!

Change:

     alert( objXMLDOC.parseError.reason );

to:

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

Author Comment

by:tahirjadoon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Try:

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

Author Comment

by:tahirjadoon
Comment Utility
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
Comment Utility
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
Comment Utility
Well thats good. Can you just restate what else (if anything) you want to do with it next ?
0
 

Author Comment

by:tahirjadoon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
toggle checkboxes when a checkbox is checked 16 47
ASP Classic - Load test 2 33
Summernote required 3 56
Select2 jquery help 9 41
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…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now