FilterChildElementsByAttrRegEx (QTP, TestComplete, XML, VBScript)
Parent page: Service Functions – MSXMLDOM (QTP, TestComplete, VBScript)
Description
Filters parent XML object by removing child XML nodes not matching specified attribute regex value.
Implementation
'Removes non-matching nodes
Public Function FilterChildElementsByAttrRegEx(ByRef objXMLParent, ByVal sTagName, ByVal sAttrName, ByVal sAttrRegex, ByVal objParameter)
Dim objColl, objNode, objRNode
Dim Iter, sValue
Dim boolMatchCase, boolRC
If objXMLParent is Nothing Then
Set FilterChildElementsByAttrRegEx = Nothing
Exit Function
End If
If TypeName(objParameter) <> "Dictionary" Then
Set objParameter = CreateObject("Scripting.Dictionary")
End If
boolMatchCase = InitBool(Trim(objParameter.Item("p.matchcase")), False)
If Not boolMatchCase Then
sAttrRegex = LCase(sAttrRegex)
End If
Set objColl = objXMLParent.SelectNodes("./"&sTagName)
For Iter = 0 To objColl.Length-1
Set objNode = objColl.Item(Iter)
Call GetXMLElementProperty(objNode, "#ATTR:" & sAttrName, -1, sValue)
If boolMatchCase Then
boolRC = Regex_Test(sValue, sAttrRegex)
Else
boolRC = Regex_Test(LCase(sValue), sAttrRegex)
End If
If boolRC Then
'keep it
Else
Set objRNode = objXMLParent.RemoveChild(objNode)
Set objRNode = Nothing
End If
Next
Set FilterChildElementsByAttrRegEx = objXMLParent
End Function
Unit Tests
'Unit tests use other functions from the parent library
'Reporting example is done in TestComplete syntax
Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "alpha", "System Step 1")
Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "betta", "System Step 2")
Set objXMLNode = SetChildElementByAttr(objXMLParent, "step", "gamma", "System Step 3")
Set objXMLParent1 = CopyChildElements(objXMLParent, Nothing)
Call FilterChildElementsByAttrRegEx(objXMLParent1, "step", "name", ".*a.*a", Nothing)
Set objXMLNode = objXMLParent1.SelectNodes("./step").item(0)
boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
boolRC = boolRC AND (sValue = "System Step 1")
If Not boolRC Then
Log.Error "FilterChildElementsByAttrRegEx failed"
End If
Set objXMLNode = objXMLParent1.SelectNodes("./step").item(1)
boolRC = GetXMLElementProperty(objXMLNode, "#text", -1, sValue)
boolRC = boolRC AND (sValue = "System Step 3")
If Not boolRC Then
Log.Error "FilterChildElementsByAttrRegEx failed"
End If

