I'm new to SO--I hope I'm posting this in the right place.
As an update to my post below, I've found one way of getting the City portion of the address. I don't really need the max value as suggested in my original post; I just need the last value because the "xyz:sequenceNumber" values are always, well, in sequence. So I tried this:
tmpCity = xNode.selectSingleNode("//abc:Person//xyz:Region").previousSibling.Text
and it seems to work, since I'm dealing with well-formatted .xml files in which the previousSibling of xyz:Region is always the last line of xyz:AddressText containing the City value that I'm looking for. I'd still appreciate any comments, because I remain in the dark as to whether this (and the code below) is even remotely efficient. I've got many large .xml files to shred, so efficiency matters.
I need to work with some obsolete VB6 code which includes a recursive XML shredding subroutine. I'm familiar with VB6, but I don't understand this subroutine. I've pasted a portion of the code below. I'm hoping someone can point me to some detailed background reading material that will help me figure out how this subroutine's XML handling aspects work so I can maintain and modify it. I've also pasted below a [sanitized] sample extract from two of the XML files that I need to work with. One problem is that the City portion of the address is stored in the last one of a series of enumerated attributes. To get the City, I need to extract the text from the attribute with the maximum xyz:sequenceNumber value. SO has many posts with examples of how to get the highest value attribute, but I can't get them to work in this subroutine. Typically, they seem to use either a max() function--which VB6 complains about when I try to use it in this subroutine; or they use something like you see in the snippet below, but when I try to adapt that VB6 complains about the double colon ("::").
doc.SelectSingleNode("//Employees/Employee/@Id[not(. <=../preceding-sibling::Employee/@id) and not(. <=../following-sibling::Employee/@Id)]");
I'm guessing that the examples I've seen pertain to different libraries than whatever is available in VB6.
Here's the XML sample:
<abc:Person>
<xyz:LicenseNo>1234</xyz:LicenseNo>
<xyz:Language xyz:languageCode="en">
<xyz:Name>
<xyz:Company xyz:languageCode="en">ABC Company Ltd.</xyz:Company>
</xyz:Name>
<xyz:AddressCollection>
<xyz:Address>
<xyz:SequencedAddress xyz:languageCode="en">
<xyz:AddressText xyz:sequenceNumber="1">The ABC Building</xyz:AddressText>
<xyz:AddressText xyz:sequenceNumber="2">123 Main Street</xyz:AddressText>
<xyz:AddressText xyz:sequenceNumber="3">3rd Floor</xyz:AddressText>
<xyz:AddressText xyz:sequenceNumber="4">Tampa</xyz:AddressText>
<xyz:Region xyz:RegionCategory=“State”>FL</xyz:Region>
<xyz:CountryCode>US</xyz:CountryCode>
<xyz:ZipCode>33607</xyz:ZipCode>
</xyz:SequencedAddress>
</xyz:Address>
</xyz:AddressCollection>
</xyz:Language>
</abc:Person>
<abc:Person>
<xyz:LicenseNo>567</xyz:LicenseNo>
<xyz:Language xyz:languageCode="en">
<xyz:Name>
<xyz:Company xyz:languageCode="en">XYZ Industries Ltd.</xyz:Company>
</xyz:Name>
<xyz:AddressCollection>
<xyz:Address>
<xyz:SequencedAddress xyz:languageCode="en">
<xyz:AddressText xyz:sequenceNumber="1">XYZ Factory Plaza</xyz:AddressText>
<xyz:AddressText xyz:sequenceNumber="2">678 Elm Street</xyz:AddressText>
<xyz:AddressText xyz:sequenceNumber="3">Orlando</xyz:AddressText>
<xyz:Region xyz:RegionCategory=“State”>FL</xyz:Region>
<xyz:CountryCode>US</xyz:CountryCode>
<xyz:ZipCode>32814</xyz:ZipCode>
</xyz:SequencedAddress>
</xyz:Address>
</xyz:AddressCollection>
</xyz:Language>
</abc:Person>
Here's the code portion:
Public Sub ShredXML(ByRef Nodes As MSXML2.IXMLDOMNodeList)
Dim xNode As MSXML2.IXMLDOMNode
For Each xNode In Nodes
If xNode.nodeType = NODE_ELEMENT Then
If xNode.nodeName = "abc:Person" Then
tmpCompany = xNode.selectSingleNode("//abc:Person//xyz:Company").Text
tmpLicenseNo = xNode.selectSingleNode("//abc:Person//xyz:LicenseNo").Text
tmpLanguage = xNode.selectSingleNode("//abc:Person//xyz:Language").Attributes.getNamedItem("xyz:languageCode").Text
tmpRegion = xNode.selectSingleNode("//abc:Person//xyz:Region").Text
tmpCountryCode = xNode.selectSingleNode("//abc:Person//xyz:CountryCode").Text
tmpZipCode = xNode.selectSingleNode("//abc:Person//xyz:ZipCode").Text
‘ database insert code omitted
End If
End If
If xNode.hasChildNodes Then
ShredXML xNode.childNodes
End If
Next xNode