?
Solved

Using XPath to test if XML node exists

Posted on 2004-09-15
26
Medium Priority
?
1,114 Views
Last Modified: 2012-06-27
Using ASP, I'm trying to access specific XML nodes with .SelectSingleNode("//TAGNAME") and test if the tag exists. Accessing the nodes (when they exist) is no problem, but the test for existance has me stumped.
I tried:
set thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")
      If StrComp("thisNode.Text", "" )<> 0 Then
            Response.Write thisNode.Text
      End If
but it gives me an 'object required' error

Should I use getElementsByTagName() instead?

I'm sure this is not a very difficult question for you experts, but I'm stuck on this and a quick solution is very important, so I'm assigning more points.
Thanks
0
Comment
Question by:Ezog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
26 Comments
 
LVL 13

Expert Comment

by:davidlars99
ID: 12066057
do not use "set" (and maybe """ around thisNode.Text)
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12066095
and make sure you have

> Dim xmlDoc   As New XML.XMLDocument

before you do anything
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12066126
or
> Set xmlDoc=Server.CreateObject("microsoft.xmldom")
0
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
LVL 13

Expert Comment

by:davidlars99
ID: 12066184
if none of these help try

> thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME").Text

that's all I could think of.. :)
0
 

Author Comment

by:Ezog
ID: 12066289
I have:
set xmlDocL = CreateObject("Microsoft.XMLDom")
xmlDoc.async = False
Set xmlDoc = objXMLHTTP.responseXML

If I use
thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME").Text

I still get the 'object reqiured' error
0
 

Author Comment

by:Ezog
ID: 12066671
when I tried:

set objElements = xmlDoc.getElementsByTagName("TAGNAME")
        If objElements.Item(0) Is Nothing Then
              thisNode = ""           
        Else
             thisNode = objElements.Item(0).nodeTypeString
        End If
Response.Write thisNode

I get 'element' as response instead of the string value
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12067195
what line is that you getting error on..?
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12067248
> set xmlDocL = CreateObject("Microsoft.XMLDom")
 what's  xmlDoc-"L " shouldn't it be  xmlDoc
0
 

Author Comment

by:Ezog
ID: 12067613
the "L" - just a typo,

I get the error on the Response.Write line
0
 

Author Comment

by:Ezog
ID: 12067864
I also tried:

set thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")
If len (thisNode.Text) >0 Then
       Response.Write thisNode.Text
End If

I get an error only when the tag does not exist.
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069190
try
> Set xmlDoc = CreateObject("Msxml2.DOMDocument")

and something very important

> Set xmlDoc = objXMLHTTP.responseXML
after this xmlDoc is not an object anymore...



I think that correct way would be following...


Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load(path_to_the_same_file_which_is_loaded_into_objXMLHTTP)

set thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")
If len (thisNode.Text) >0 Then
       Response.Write thisNode.Text
End If





0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069224
when you try "thisNode.Text" it's looking for the object, but with "Set xmlDoc = objXMLHTTP.responseXML"    "xmlDoc"  would never be the object
0
 

Author Comment

by:Ezog
ID: 12069521
I inserted
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
and replaced
    Set xmlDoc = objXMLHTTP.responseXML
with
    xmlDoc.load("http://URL")
path_to_the_same_file_which_is_loaded_into_objXMLHTTP

but get:
Microsoft VBScript runtime error '800a01a8'
Object required
/response.asp, line 49
which is:
    set thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")

Also, I only received an error if the tag is missing.  If the tag exists, I can enter its value into the form with the following:
     Set rootNode = xmlDoc.documentElement
     Set thisNode = rootNode.SelectSingleNode("//TAGNAME")
      Response.Write thisNode.Text
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069663
how about


Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load(path_to_the_same_file_which_is_loaded_into_objXMLHTTP)
set xmlResponse=xmlDoc.responseXML  'also try without "set"

thisNode = xmlResponse.documentElement.SelectSingleNode("//TAGNAME")
If len (thisNode.Text) >0 Then
       Response.Write thisNode.Text
End If



0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069727
ok, ignore my last post and try this first


Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load(path_to_the_same_file_which_is_loaded_into_objXMLHTTP)

thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")
If len (thisNode.Text) >0 Then
       Response.Write thisNode.Text
End If



is loading file XML or some other type..?
0
 

Author Comment

by:Ezog
ID: 12069747
I get with and without "set":
'Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'responseXML'
/response.asp, line 27
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069815
did you try one above your post..?
0
 

Author Comment

by:Ezog
ID: 12069852
I'm getting a feed with about 30 possible tags but only about half of them are returned at any given time.  I don't want to use stylesheets so I try to find the missing tags with conditionals...
maybe I'm going about it the wrong way
0
 

Author Comment

by:Ezog
ID: 12069885
hm, I don't understand that comment.  What do you mean with "above your post"?
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12069963
I mean this one


Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.load(path_to_the_same_file_which_is_loaded_into_objXMLHTTP)

thisNode = xmlDoc.documentElement.SelectSingleNode("//TAGNAME")
If len (thisNode.Text) >0 Then
       Response.Write thisNode.Text
End If
0
 

Author Comment

by:Ezog
ID: 12069998
I think I might have figured it out:
This one works with the old
    Set xmlDoc = objXMLHTTP.responseXML

set objElements = objXML.selectSingleNode("//ZONING")
        if objElements is nothing then
        Response.Write "(Not available)"
    else
        Response.Write objElements.Text
    end if
0
 
LVL 13

Accepted Solution

by:
davidlars99 earned 1000 total points
ID: 12070012
so you got it working..?
0
 

Author Comment

by:Ezog
ID: 12070025
actualy, I rewrote the thing and used objXML instead of xmlDOC (just for reference or the code would not make sence!)
0
 

Author Comment

by:Ezog
ID: 12070058
I think it works... at least so far, but I would like to leave the treat open until I have done some more testing. Thank you so much for you help! I will award your points on closing.
0
 
LVL 3

Expert Comment

by:gnudiff
ID: 12072906
> Using XPath to test if XML node exists

Is there any reason for not using simply SelectSingleNode("count(//TAGNAME)") Xpath? It should return the count of TAGNAME nodes, 0 - if no such nodes exist...

0
 

Author Comment

by:Ezog
ID: 12074671
I tried the SelectSingleNode("count(//TAGNAME)") and got the following error:

    msxml3.dll error '80004005'
    Unknown method. -->count(//<--STATE)

0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

762 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