Solved

If Node Contains..

Posted on 2003-11-26
19
395 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
  • 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update table value with join syntax 2 69
Select distinct 25 100
Change visitor's REMOTE_ADDR to server's REMOTE_ADDR 2 30
Index on a Table 6 14
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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