Solved

If Node Contains..

Posted on 2003-11-26
19
387 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
Comment Utility
<%@ 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
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:deighc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OK that works, what if I had 2 words to check?

Appreciate all the help

Bill D
0
 
LVL 15

Expert Comment

by:deighc
Comment Utility
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
Comment Utility
should "then then" be just "then" at the end of the "then" statement?

Thanks
Bill D
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Author Comment

by:B_Dorsey
Comment Utility
Also can the "and" be a  "or"?

Thanks
Bill D
0
 
LVL 15

Expert Comment

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

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

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now