I am trying to write a VBA script that works as a macro for an Excel file and translates this to an XML file. This I have been able to achieve however it needs to check its own data on the fly to prevent double notations. The data is sorted based on a marker called a tagname. There exists the possibility that a certain tagname exist more than once.
What I would like to be able to do is parse the XML document that is being created based on this tagname and when a node with this tagname is found, the additional information should be appended to the info attribute of this node.
Currently, the XML hat is being produced looks like this:
<Section name="GS.0101PS02">
<Translation key="Info">=GS+MCC-151B9 Filterbewaking</Translation>
</Section>
<Section name="GS.0102PS02">
<Translation key="Info">=GS+MCC-152B3 Filterbewaking</Translation>
</Section>
<Section name="GS.0025LS01">
<Translation key="Info">=GS+MCC-161B5 Niveau opvangbak</Translation>
</Section>
<Section name="GS.0026LS01">
<Translation key="Info">=GS+MCC-161B15 Niveau opvangbak</Translation>
</Section>
<Section name="GS.0300PS02">
<Translation key="Info">=GS+MCC-162B11 Filterbewaking</Translation>
</Section>
<Section name="GS.0141AV05">
<Translation key="Info">=GS+CC1-150B3 Bunker 1 Eindklep Open <br/> =GS+CC1-150B5 Bunker 1 Eindklep Dicht</Translation>
</Section>
The last line is an example of the tagname that exists multiple times throughout the document and this is also how it should be formatted. Currently the code is only capable of doing this when the tagname repeats itself in the next row, however, this is not a given.
I have attempted realizing this using selectnodes()
and selectsinglenode()
and a couple other approached but have been unsuccessful the main reason I can think of is that my my XPath if formatted improperly.
The script I am running is as follows:
Sub RPCTranslatesCombinedInfoBackwardsChecking()
Set oXMLDoc = CreateObject("MSXML2.DOMDocument") 'Create the XML document'
Set oPI = oXMLDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8""") 'Define the processing instructions'
Set oRoot = oXMLDoc.CreateNode(1, "Translations", "urn:Riwo.Pcs.Localization") 'Create the root node "Translations" in the name space urn:Riwo.Pcs.Localization'
Set xsi_Attribute = oXMLDoc.CreateAttribute("xmlns:xsi") 'Add the first attribute to the namespace'
xsi_Attribute.Value = "http://www.w3.org/2001/XMLSchema-instance"
oRoot.Attributes.setNamedItem (xsi_Attribute)
Set xsd_Attribute = oXMLDoc.CreateAttribute("xmlns:xsd") 'Add the Second attribute to the namespace'
xsd_Attribute.Value = "http://www.w3.org/2001/XMLSchema"
oRoot.Attributes.setNamedItem (xsd_Attribute)
Set code_Attribute = oXMLDoc.CreateAttribute("code") 'Add the Third attribute to the namespace'
code_Attribute.Value = "nl"
oRoot.Attributes.setNamedItem (code_Attribute)
Set description_Attribute = oXMLDoc.CreateAttribute("description") 'Add the Fourth attribute to the namespace'
description_Attribute.Value = "Dutch"
oRoot.Attributes.setNamedItem (description_Attribute)
oXMLDoc.AppendChild oRoot 'Append the above defined node to the document'
oXMLDoc.InsertBefore oPI, oXMLDoc.ChildNodes.Item(0)
With ActiveSheet
lRow = 2 'Start the macro operation at the second row of the active worksheet'
sSectionName = ""'Initialize the sectionname string'
Do While .Cells(lRow, 4).Value <> ""'While there is a value in the fourth colom, starting at row 2'
sLineName = .Cells(lRow, 1).Value 'Define a prefix for the section names'
sSectionPrefix = Right(sLineName, Len(sLineName) - 1)
Passes = 0
sSectionName = .Cells(lRow, 4).Value 'Define the value of row x in colom 4 as the section name'
Set oElmSection = oXMLDoc.CreateNode(1, "Section", "urn:Riwo.Pcs.Localization") 'Create the section node'
oXMLDoc.DocumentElement.AppendChild oElmSection 'Append the section node to the document'
Set oAttr = oXMLDoc.CreateNode(2, "name", "urn:Riwo.Pcs.Localization") 'Add the name attribute to the section node'
NodeName = sSectionPrefix & "."& sSectionName
oAttr.NodeValue = NodeName 'Define the name of the section node as being the prefix aswel as the section name from the fourth colom'
oElmSection.SetAttributeNode oAttr 'Set the name attribute as an attribute of section'
ExistingNodes = oXMLDoc.SelectNodes("//Section").Attributes.getNamedItem([@name=NodeName]).Text
Do While .Cells(lRow, 4).Value = sSectionName 'For all sections with the same section name do:'
sInfoDescription_1 = .Cells(lRow, 1).Value 'Fetch the info data for the respective row'
sInfoDescription_2 = .Cells(lRow, 2).Value
sInfoDescription_3 = .Cells(lRow, 3).Value
sInfoDescription_5 = .Cells(lRow, 5).Value
If Passes = 0 Then 'Check if first pass of the section"
sInfo = sInfoDescription_1 & sInfoDescription_2 & sInfoDescription_3 & ""& sInfoDescription_5 'Combine info in 1 string'
Set oElmTranslation = oXMLDoc.CreateNode(1, "Translation", "urn:Riwo.Pcs.Localization") 'Create Translation node'
Set oAttr = oXMLDoc.CreateNode(2, "key", "urn:Riwo.Pcs.Localization") 'Add key attribute'
oAttr.NodeValue = "Info"'Define Info as key'
oElmTranslation.SetAttributeNode oAttr 'Set key attribute in Translation node'
oElmTranslation.AppendChild oXMLDoc.createTextNode(sInfo) 'Use info text as info'
oElmSection.AppendChild oElmTranslation 'Append Translation node to section'
Passes = 1
lRow = lRow + 1 'Procede to next row'
Else 'Second or more passes over a section'
sInfo = "<br/> "& sInfoDescription_1 & sInfoDescription_2 & sInfoDescription_3 & ""& sInfoDescription_5 'Combine info in 1 string'
oElmTranslation.AppendChild oXMLDoc.createTextNode(sInfo) 'Use info text as info'
lRow = lRow + 1 'Procede to next row'
End If
Loop 'Loop second while'
Loop 'Loop first while'
End With
MsgBox oXMLDoc.XML 'Show result'
oXMLDoc.Save "C:\Users\thomas.RIWO\Desktop\Translations\RPC test\test2.xml"'Save the xml file'
End Sub
What I would like to know is how to retrieve an existing node from my xml and edit this.