Solved

If Node Contains..

Posted on 2003-11-26
19
390 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

4 Experts available now in Live!

Get 1:1 Help Now