0

I've got the following XML code:

   <OrganisationInfo>
       <NetworkList>
          <Network>
             <NetworkData>
                <RoutingInfoSection>
                   <ChangeHistory>
                      <ChangeHistoryItem>
                         <Date>2013-06-04</Date>
                         <Description>BLABLABLA</Description>
                      </ChangeHistoryItem>
                      <ChangeHistoryItem>
                         <Date>2013-05-21</Date>
                         <Description>BLABLABLA</Description>
                      </ChangeHistoryItem>
                   </ChangeHistory>
                </RoutingInfoSection>
             </NetworkData>
          </Network>
       </NetworkList>
   </OrganisationInfo>

I have done a VBScript that is able to read xml files in a directory, get some nodes values and save them to a txt file until now, but I don't want to get all the values in the "Date" Node... My function below saves every value assigned to Operadora and "Alteracao", on "Operadora & ";" & Alteracao", but how can I change my code so it get only the most recent Date that exists?

I mean: Some XML come with the most recent Date in the first position, some of them in the last, and some of them in the middle... How could I get the most recent, wherever it is?

I want the Date with the most recent year (2014, if there are Dates with 2014, 2013, 2012, 2011...), with the most recent month (12, if there are months 12, 06, 08 or 11, for example) and so on with the most recent day.

Follows my code until now:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0") 'Msxml2.DOMDocument / Microsoft.XMLDOM    
     xmlDoc.Async = "False"
     xmlDoc.setProperty "SelectionLanguage", "XPath"

            Function ExportaDados
                For Each f In fso.GetFolder("C:\Users\f8057612\Desktop\Bancos\Script_Operadoras").Files
                    If LCase(fso.GetExtensionName(f)) = "xml" Then
                        xmlDoc.Load f.Path

                        If xmlDoc.ParseError = 0 Then
                        For Each OrganisationInfo In xmlDoc.SelectNodes("//OrganisationInfo/OrganisationName")
                            Operadora = OrganisationInfo.Text
                            temp = ""

                            For Each Alteracao_Dir In xmlDoc.SelectNodes("//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date")
                                If  Alteracao_Dir.Text <> temp Then
                                    temp = Alteracao_Dir.Text
                                    Alteracao = Alteracao_Dir.Text
                                    objetoSaida_Alteracao.WriteLine Operadora & ";" & Alteracao
                                End If
                                temp = Alteracao_Dir.Text
                            Next
                        Next
                        WScript.Echo "Parsing error: '" & f.Path & "': " & xmlDoc.ParseError.Reason
                        End If
                     End If
                Next
             End Function

1 Answer 1

1

Since your date values are in ISO format they can be compared/ordered correctly even as strings, so you could simply do something like this:

xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date"

Set mostRecent = Nothing
For Each node In xmlDoc.SelectNodes(xpath)
  If mostRecent Is Nothing Then
    Set mostRecent = node
  ElseIf node.Text > mostRecent.Text Then
    Set mostRecent = node
  End If
Next

If Not mostRecent Is Nothing Then
  WScript.Echo "The most recent date is: " & mostRecent.Text
End If

After the loop terminates the variable mostRecent is Nothing when there wasn't a <Date> node, otherwise it holds the <Date> node with the most recent date.

Sign up to request clarification or add additional context in comments.

5 Comments

Woa, the language lets us compare a String with "<", ">" operations? O.O I will test it and post!
@CharlieVelez Yes. Strings are compared by ASCII character order, so "1" < "2". But beware, that also means "10" < "2"!
Yes, but 2014-03-01 is higher than 2014-10-01 to the language, isn't it?
@CharlieVelez No. "2014-0" < "2014-1", regardless of what the 7th character is.
To realize the power/applicability of Ansgar's technique, look at stackoverflow.com/a/15301674/603855 and stackoverflow.com/a/18067346/603855 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.