1
votes

I have the following type XML file:

<ECSC>
   <ATTRIBUTES>
        < all child nodes of attribute tag >
   </ATTRIBUTES>
   <SCRIPT>
      <ETXML_LINE_TABTYPE>
          <item>MESSAGE ( MSG_1 ).</item>
    <item>* To get the name of the Title Screen.</item>
    <item>  GETGUI ( FB01_100_STEP_1 ).</item>

    <item>  SAPGUI ( FB01_100_STEP_2 ).</item>
    <item>* Enter Amount and Tax Code.</item>
    <item>* and, Press F4 help in the Order Field.</item>
    <item>  SAPGUI ( FB01_300_STEP_1 ).</item>
    <item>* In F4 screen, enter the 'External Order Number'</item>
    <item>* pop-up screen is displayed with entries like Order, Description and External Order Number and select 1st order row, press Enter.</item>
    <item>  SAPGUI ( FB01_200_STEP_1 ).</item>
    <item>* To get the values for the field 'Order, Description and External Order No' from F4 help.</item>
    <item>  GETGUI ( FB01_120_STEP_1 ).</item>
    <item>* Press 'Enter' button.</item>
    <item>  SAPGUI ( FB01_120_STEP_3 ).</item>
    <item>* To get the value for the field 'Order' from Main screen.</item>
    <item>  GETGUI ( FB01_300_STEP_2 ).</item>
    <item>* click on 'F3' back button.</item>
    <item>  SAPGUI ( FB01_300_STEP_3 ).</item>
    <item>* click on 'F3' back button.</item>
    <item>  SAPGUI ( FB01_700_STEP_1 ).</item>
    <item>* click 'Yes' button.</item>
    <item>  SAPGUI ( FB01_200_STEP_2 ).</item>
    <item>* click on 'F3' back button.</item>
    <item>  SAPGUI ( FB01_100_STEP_3 ).</item>
    <item>ENDMESSAGE ( E_MSG_1 ).</item>
    <item/>
    <item>* To display the Title Screen.</item>
    <item>  LOG ( V_TITLE_SCREEN ).</item>
    <item>* To display the 'Order' Number from F4 help.</item>
    <item>  LOG ( V_ORDER_NO_FROM_F4 ).</item>
    <item>* To display the 'Description' from F4 help.</item>
    <item>  LOG ( V_DESCRIPTION_FROM_F4).</item>
    <item>* To display the 'External Order no' value from F4 help.</item>
    <item>  LOG ( V_EXTERNAL_ORDER_NO_FROM_F4 ).</item>
    <item>* To display the 'Order' Number from main screen.</item>
    <item>  LOG ( V_ORDER_NO_FRM_MAIN_SCREEN ).</item>
    <item>*********************************************************************.</item>
    <item>* End Execution.</item>
    <item>*********************************************************************.</item>
    <item/>
    <item>*******************************************************************.</item>
    <item>* Check.</item>
    <item>********************************************************************.</item>
    <item>* To check name of Title screen for transaction FB01.</item>
    <item>  CHEVAR ( V_TITLE_SCREEN = I_TITLE_SCREEN ).</item>
    <item>* To check the value for the field 'External Order No' from F4 help, which should be equal to 'External Order No' from table.</item>
    <item>  CHEVAR ( V_EXTERNAL_ORDER_NO_FRM_TABL = V_EXTERNAL_ORDER_NO_FROM_F4 ).</item>
    <item>* To check the values for the field 'Order' number from Table, which should be equal to 'Order' no from F4 screen and Main screen.</item>
    <item>  CHEVAR ( ( I_ORDER_NUMBER_FROM_TABLE = V_ORDER_NO_FROM_F4 ) AND ( I_ORDER_NUMBER_FROM_TABLE = V_ORDER_NO_FRM_MAIN_SCREEN )).</item>
    <item>*******************************************************************.</item>
    <item>* End Check.</item>
    <item>********************************************************************.</item>
    </ETXML_LINE_TABTYPE>
   </SCRIPT>
  <PARAMETERS>
        <all childnodes of parameter of parameters tag >
  </PARAMETERS>
  </ECSC>

We want to select a particular node like <SCRIPT> and its all child nodes like all <item> nodes from above XML; we want to detect such nodeText having doesn't have any comment mentioned. Comment is denoted by line which is starting from character * and each comment line is present just above the code line; for example in above XML file, the line: <item> SAPGUI ( FB01_100_STEP_2 ).</item> doesn't have any comment so it is a defect, and I tried the following vbscript.

Dim sFSpec   : sFSpec       = "D:\new Automation\backup\script.xml"
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.load sFSpec
If 0 = objMSXML.parseError Then
 Dim bCmnt : bCmnt = False
 Dim ndItem
 For Each ndItem In objMSXML.documentElement.childNodes
     Dim sItem : sItem = ndItem.text
     If "*" = Left(sItem, 1) Then
        bCmnt = True
     Else
        If Not bCmnt Then
          If "" <> sItem And 0 = Instr(sItem, "MESSAGE") Then
             msgbox "no comment for:", sItem
           End If
        End If
        bCmnt = False
     End If
 Next
Else
   msgbox objMSXML.parseError.reason
End If

Please help me. Thank You in advance.

1
What do you mean by "we want to detect such nodeText having doesn't have any comment mentioned." ? It's pretty confusing. - Neeku

1 Answers

2
votes

A question like "select every <item> that is not preceded by a certain type of element" is a perfect candidate for XPath.

//SCRIPT//item[
  string-length(normalize-space(.)) > 0
  and substring(normalize-space(.), 1, 1) != '*'
  and substring(normalize-space(preceding-sibling::item[1]), 1, 1) != '*'
]

i.e.

  • any <item> beneath <SCRIPT> that
  • is not empty
  • and whose first non-whitespace character is not a star
  • and whose immediately preceding <item> does not have a star either

in VBScript:

Option Explicit

Dim XmlDoc
Dim DefectiveLines, Line, LineNum

Set XmlDoc = CreateObject("MSXML2.DOMDocument")

XmlDoc.setProperty "SelectionLanguage", "XPath"
XmlDoc.async = False
XmlDoc.load "D:\new Automation\backup\script.xml"

If XmlDoc.parseError = 0 Then
  Set DefectiveLines = XmlDoc.SelectNodes( _
    "//SCRIPT//item[" & _
      "string-length(normalize-space(.)) > 0" & _
      "and substring(normalize-space(.), 1, 1) != '*'" & _
      "and substring(normalize-space(preceding-sibling::item[1]), 1, 1) != '*'" & _
    "]" _
  )

  For Each Line In DefectiveLines
    LineNum = Line.SelectNodes("./preceding-sibling::item").Length + 1
    WScript.Echo "Error for line " & LineNum & ": " & Line.Text
  Next
Else
   WScript.Echo XmlDoc.parseError.reason
End If

which gives me

  • Error for line 1: MESSAGE ( MSG_1 )
  • Error for line 4: SAPGUI ( FB01_100_STEP_2 ).
  • Error for line 25: ENDMESSAGE ( E_MSG_1 ).

Note that I've switched on Option Explicit, something you should get into the habit of doing. I also used more sensible variable names.