Solved

If Node Contains..

Posted on 2003-11-26
19
403 Views
Last Modified: 2008-03-06
Is there a way that I can check if a node contains a certain word or character string?

For example my xml is something like
<xml>
  <item>sd as asda sd</item>
  <item>sdsds sdf sdf</item>
  <item>dsf asd asd</item>
</xml>

Basically in ASP I want to check to see if for instance "sdsds" is in the node somewhere.

Thanks
Bill D
0
Comment
Question by:B_Dorsey
[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
  • 10
  • 8
19 Comments
 
LVL 21

Expert Comment

by:ap_sajith
ID: 9827889
<%@ language="vbscript" %>
<% Option Explicit %>
<%
    Dim xmlDoc, root, success
    Set xmlDoc = Server.CreateObject("msxml2.DOMDocument")
    ' Allow the document to complete loading
    xmlDoc.async = False
    success = xmlDoc.Load(Server.MapPath("xml/cd.xml"))
    If success = True Then
        Set root = xmlDoc.documentElement
        displayNode root
        Set root = Nothing
    End If
    Set xmlDoc = Nothing
Private Sub displayNode(ByVal node)
    Dim child, strName, strData
    strName = node.nodeName
    strData = node.Text
    If Instr(strData,"sdsds")>0 Then ' >> Check for the string <<
    Response.Write strName & ": " & strData & "<br />"
    end if
    ' Check for child nodes
    If node.childNodes.length > 1 Then
        For Each child In node.childNodes
            displayNode child
        Next
    End If
End Sub
%>

Hope this helps.. cheers!!
0
 
LVL 15

Expert Comment

by:deighc
ID: 9830992
To get a list of ALL nodes that contain the string:

(this example requires an XML DOM object - the first 6 or 7 lines of ap_sajith's example show how to create this)

function nodeListFromNodeText(ByRef xmlDOM, searchText)
  dim xmlNodeList
  set xmlNodeList = xmlDOM.selectNodes("/xml/item[contains(text(), '" & searchText & "')]")
  nodeListFromNodeText = xmlNodeList
end function

This will return a node list object (which is simply a collection of node objects) that you can loop thru and do whatever you want with. If no nodes are found the node list will be empty (ie. the length property of the object will be 0).
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9857214
Sorry for taking long to get back to this, can something be worked out for my code below?\

Basically I want to check if the word "SCAMMED" is in the title node, if its not, then display the node result and move to the next one, if it does, then skip over it.

Thanks
Bill D

---CODE----
Set setexternallists = objexternallists.selectNodes("/results/hits/hit")
      for each objexternallist in setexternallists
            iexternal = iexternal + 1
            Response.Write "<result>"
            Response.Write ("<title><![CDATA[" & objexternallist.selectSingleNode("title").text & "]]></title>")
            trimmedtitle = objexternallist.selectSingleNode("title").text
            trimmedtitle = Replace(trimmedtitle, "<span class=""hl"">", "")
            trimmedtitle = Replace(trimmedtitle, "</span>", "")
            Response.Write ("<trimmedtitle><![CDATA[" & trimmedtitle & "]]></trimmedtitle>")
            Response.Write "</result>"

      next
---CODE----
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 15

Expert Comment

by:deighc
ID: 9857259
Well I can't provide a guarenteed answer without seeing the source XML you're working with. But assuming it looks something like this:

<results>
  <hits>
    <hit>
      <title></title>
    </hit>
     ...
  </hits>
</results>

Try using this XPATH query to get your node list:

Set setexternallists = objexternallists.selectNodes("/results/hits/hit[not(contains(./title/text(), 'SCAMMED'))]")

This will return a nodeList if <hit> nodes whose <title> child node DOESN'T have 'SCAMMED' in the node text.

If that's not right then post a sample of the XML you're working with.
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9857483
I get an error

Unknown method. /results/hits/hit[not(-->contains(.<--/title/text(),'Scammed'))]

Structure is exactly how you have it above.


Thanks
bill D
0
 
LVL 15

Accepted Solution

by:
deighc earned 500 total points
ID: 9857967
Oops, it seems that the 'contains' XPATH function is only available for XSL transformations, not queries on an XML DOM. Weird. So just make a small modification to your code snippet:

Set setexternallists = objexternallists.selectNodes("/results/hits/hit")
     for each objexternallist in setexternallists
          if instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "SCAMMED") = 0 then
            iexternal = iexternal + 1
            Response.Write "<result>"
            Response.Write ("<title><![CDATA[" & objexternallist.selectSingleNode("title").text & "]]></title>")
            trimmedtitle = objexternallist.selectSingleNode("title").text
            trimmedtitle = Replace(trimmedtitle, "<span class=""hl"">", "")
            trimmedtitle = Replace(trimmedtitle, "</span>", "")
            Response.Write ("<trimmedtitle><![CDATA[" & trimmedtitle & "]]></trimmedtitle>")
            Response.Write "</result>"
        end if
     next

ie. just do a string compare on the node text (which is what ap_sajith originally suggested). Sorry for the detour ;-)

0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858110
OK that works, what if I had 2 words to check?

Appreciate all the help

Bill D
0
 
LVL 15

Expert Comment

by:deighc
ID: 9858468
if instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 2>") = 0 and _
     instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 1>") = 0 then then


Add as many conditions to the if clause as you need. If you want it so that the check INCLUDES the word, use

instr([find], [source]) > 0

(instr returns the position of the first occurance [find] in [source]. If [find] doesn't appear in [source] then it returns 0. So if a number greater than 0 is returned you know the [find] must be a substring of [source])
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858491
should "then then" be just "then" at the end of the "then" statement?

Thanks
Bill D
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858494
Also can the "and" be a  "or"?

Thanks
Bill D
0
 
LVL 15

Expert Comment

by:deighc
ID: 9858515
>> should "then then" be just "then" at the end of the "then" statement?

Yes. Just one. Silly typo from me.

>> Also can the "and" be a  "or"?

Absolutely.
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858554
Hmmm Im stumped...

I try this "if instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 2>") = 0 then"

and it works fine...

I try this "if instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 2>") = 0 or instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 1>") = 0 then"

And it doesnt do anything

Any ideas?

Thanks
Bill D

0
 
LVL 15

Expert Comment

by:deighc
ID: 9858611
So, that statement will evaluate to true if the the <title> node doesn't contain either <word 1> or <word 2>. ie. it may contain one of the words (but it may not) and it definitely won't contain both of them.

My question: Is this the logic you want or is the problem that it's not working as I've described?
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858649
You lost me...

Basically I want to check if the title contains one of two words,  dont know much about the logic I want  <G> cause Im lost on my own logic

thanks
Bill D
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858660
By this statement "And it doesnt do anything" I mean that it just displays results even if they contain the word "SCAMMED" or not

Bill D
0
 
LVL 15

Expert Comment

by:deighc
ID: 9858676
>> Basically I want to check if the title contains one of two words

OK, and if it DOES contain one of the two words should it enter into the if.. then statements or should it ignore the statements between the if..then???
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858704
if it contains one of those two words then it should go onto the next result/hits/hit and see if it does if it doesnt, then do the stuff below in the script, else check the next one.

Thanks
Bill D
0
 
LVL 15

Expert Comment

by:deighc
ID: 9858743
So only nodes that don't contain EITHER word should get thru the if statement, right? Just use an 'and' instead of an 'or' :

if instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 2>") = 0 and
      instr(objexternallist.selectSingleNode("/results/hits/hit/title").text, "<word 1>") = 0 then
0
 
LVL 14

Author Comment

by:B_Dorsey
ID: 9858773
AWESOME...

Ive been looking at this "AND" or "OR" thing incorrectly... I looked at it like... "if it contains this OR this then"... instead of the other way around at it


Thanks
Bill D
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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