• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

If Node Contains..

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
B_Dorsey
Asked:
B_Dorsey
  • 10
  • 8
1 Solution
 
ap_sajithCommented:
<%@ 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
 
deighcCommented:
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
 
B_DorseyAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
deighcCommented:
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
 
B_DorseyAuthor Commented:
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
 
deighcCommented:
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
 
B_DorseyAuthor Commented:
OK that works, what if I had 2 words to check?

Appreciate all the help

Bill D
0
 
deighcCommented:
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
 
B_DorseyAuthor Commented:
should "then then" be just "then" at the end of the "then" statement?

Thanks
Bill D
0
 
B_DorseyAuthor Commented:
Also can the "and" be a  "or"?

Thanks
Bill D
0
 
deighcCommented:
>> 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
 
B_DorseyAuthor Commented:
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
 
deighcCommented:
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
 
B_DorseyAuthor Commented:
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
 
B_DorseyAuthor Commented:
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
 
deighcCommented:
>> 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
 
B_DorseyAuthor Commented:
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
 
deighcCommented:
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
 
B_DorseyAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now