Displaying XML elements in an ASP page

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 




tahirjadoonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Carl TawnSystems and Integration DeveloperCommented:
Doh, server side !!

Change:

     alert( objXMLDOC.parseError.reason );

to:

    Response.Write (objXMLDOC.parseError.reason);
0
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
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
tahirjadoonAuthor Commented:
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
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
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
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
Try:

    var objChildNodes = objXMLDoc.selectNodes("//COUNT/*[not(@AREATYPE)]");
0
tahirjadoonAuthor Commented:
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
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
Well thats good. Can you just restate what else (if anything) you want to do with it next ?
0
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
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
tahirjadoonAuthor Commented:
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
Carl TawnSystems and Integration DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carl TawnSystems and Integration DeveloperCommented:
P.S.

   That ASP code is VBScript, you will probably need to adapt it for JScript if thats what you want to use.
0
tahirjadoonAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.