问题
i want to get from my relevant Nodes all Attributes i can get. Problem is some Childnotes inherit from Parents or Global. What i want is if Childnotes attribute is missing i want it from parent or global. Tried several things nothing worked like (/ancester::) could someone help me?
My Code for all Nodes:
Sub GetAllNodes_V4_8()
Dim XmlNamespaces As String
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlNodes As MSXML2.IXMLDOMNodeList
Dim xmlAttribute As MSXML2.IXMLDOMAttribute
Dim node As MSXML2.IXMLDOMNode
Dim iRow As Integer
Dim MyRoles As Integer
Dim oTarget As Range
Dim oRegEx As VBScript_RegExp_55.RegExp
Dim oMatches As VBScript_RegExp_55.MatchCollection
Dim oMatch As VBScript_RegExp_55.Match
Dim sVarType As String
MyRoles = ThisWorkbook.Sheets("MDI OPC Items").Range("B3")
' load the file
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False: xmlDoc.validateOnParse = False
xmlDoc.Load Application.GetOpenFilename()
XmlNamespaces = "xmlns:sonic='http://opcfoundation.org/UA/2011/03/UANodeSet.xsd' " & _
"xmlns:ua='http://xxx/NodeSet.xsd' " & _
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " & _
"xmlns:uax='http://opcfoundation.org/UA/2008/02/Types.xsd' " & _
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' " & _
"xmlns:pv='http://yyy/NodeSet.xsd'"
xmlDoc.SetProperty "SelectionLanguage", "XPath"
xmlDoc.SetProperty "SelectionNamespaces", XmlNamespaces
Application.ScreenUpdating = False
oTarget.Parent.EnableCalculation = False
Set xmlNodes = xmlDoc.SelectNodes("/sonic:UANodeSet/sonic:UAVariable[contains(@NodeId, 'ns=1;s=::')]
[starts-with(@DataType, 'i=')][not(@ArrayDimensions)]//@Target")
Now im writing down the Values from xmlNodes. it goes on with:
Set xmlNodes = xmlDoc.SelectNodes("/sonic:UANodeSet/sonic:UAVariable[contains(@NodeId, 'ns=1;s=::')][starts-with(@DataType, 'i=')][not (@ArrayDimensions) ]/@DataType")
'Write this
Set xmlNodes = xmlDoc.SelectNodes("/sonic:UANodeSet/sonic:UAVariable[starts-with(@DataType, 'i=')]/sonic:Extensions/sonic:Extension/pv:VariableExtension/sonic:ACL/sonic:ACE[@Role=" & MyRoles & "]/@Allow")
Now here is the problem on the last row. Cause some of my Nodes inherite Role and Allow. Is there anything i can do to get the global one (or parent) if attribute is missing?
XML:
<?xml version="1.0" encoding="utf-8"?>
<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:ua="http://xxx/NodeSet.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pv="http://yyy/NodeSet.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd">
<UAObject NodeId="ns=1;s=::" BrowseName="1:::">
<DisplayName><Default></DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=2;i=10001</Reference>
<Reference ReferenceType="Organizes" IsForward="false">ns=2;i=20001</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::Program</Reference>
</References>
<Extensions>
<Extension>
<pv:ObjectExtension>
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
</pv:ObjectExtension>
</Extension>
</Extensions>
</UAObject>
<UAObject ParentNodeId="ns=1;s=::" NodeId="ns=1;s=::AsGlobalPV" BrowseName="1:Global PV">
<DisplayName>Global PV</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=61</Reference>
<Reference ReferenceType="Organizes" IsForward="false">ns=1;s=::</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gFahrzeug</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::AsGlobalPV:gLebewesen</Reference>
</References>
</UAObject>
<UAVariable DataType="ns=1;i=100000" ParentNodeId="ns=1;s=::AsGlobalPV" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug" BrowseName="1:gFahrzeug" AccessLevel="3" ValueRank="-1">
<DisplayName>gFahrzeug</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100005</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100010" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" BrowseName="1:Kraftfahrzeug" AccessLevel="3" ValueRank="-1">
<DisplayName>Kraftfahrzeug</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100015</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.PKW</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" BrowseName="1:LKW" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
<DisplayName>LKW</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[0]" BrowseName="1:LKW[0]" AccessLevel="3">
<DisplayName>LKW[0]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[0]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=3" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.LKW[1]" BrowseName="1:LKW[1]" AccessLevel="3">
<DisplayName>LKW[1]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.LKW[1]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=12" ParentNodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug" NodeId="ns=1;s=::AsGlobalPV:gFahrzeug.Kraftfahrzeug.PKW" BrowseName="1:PKW" AccessLevel="3" ValueRank="-1">
<DisplayName>PKW</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gFahrzeug.Kraftfahrzeug.PKW"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100020" ParentNodeId="ns=1;s=::AsGlobalPV" NodeId="ns=1;s=::AsGlobalPV:gLebewesen" BrowseName="1:gLebewesen" AccessLevel="3" ValueRank="-1">
<DisplayName>gLebewesen</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100025</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100030" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier" BrowseName="1:Tier" AccessLevel="3" ValueRank="-1">
<DisplayName>Tier</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100035</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100040" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund" BrowseName="1:Hund" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
<DisplayName>Hund</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100045</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0]</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1]</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100040" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0]" BrowseName="1:Hund[0]" AccessLevel="3" ValueRank="-1">
<DisplayName>Hund[0]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100045</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].MastinoNapoletano</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[0]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=1" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0]" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli" BrowseName="1:Xoloitzcuintli" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
<DisplayName>Xoloitzcuintli</DisplayName>
<References>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli[0]</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli[1]</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[0].Xoloitzcuintli"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=1" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli[0]" BrowseName="1:Xoloitzcuintli[0]" AccessLevel="3">
<DisplayName>Xoloitzcuintli[0]</DisplayName>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[0].Xoloitzcuintli[0]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=1" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].Xoloitzcuintli[1]" BrowseName="1:Xoloitzcuintli[1]" AccessLevel="3">
<DisplayName>Xoloitzcuintli[1]</DisplayName>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[0].Xoloitzcuintli[1]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=7" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0]" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[0].MastinoNapoletano" BrowseName="1:MastinoNapoletano" AccessLevel="3">
<DisplayName>MastinoNapoletano</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[0].MastinoNapoletano"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="ns=1;i=100040" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1]" BrowseName="1:Hund[1]" AccessLevel="3" ValueRank="-1">
<DisplayName>Hund[1]</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=1;i=100045</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1].Xoloitzcuintli</Reference>
<Reference ReferenceType="HasComponent">ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1].MastinoNapoletano</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[1]"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=1" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1]" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1].Xoloitzcuintli" BrowseName="1:Xoloitzcuintli" AccessLevel="3" ValueRank="1" ArrayDimensions="2">
<DisplayName>Xoloitzcuintli</DisplayName>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[1].Xoloitzcuintli"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAVariable DataType="i=7" ParentNodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1]" NodeId="ns=1;s=::AsGlobalPV:gLebewesen.Tier.Hund[1].MastinoNapoletano" BrowseName="1:MastinoNapoletano" AccessLevel="3">
<DisplayName>MastinoNapoletano</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<Value>
<Binding Type="PV" Target="::gLebewesen.Tier.Hund[1].MastinoNapoletano"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
<UAObject ParentNodeId="ns=1;s=::" NodeId="ns=1;s=::Program" BrowseName="1:Program">
<DisplayName>Program</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">ns=2;i=10002</Reference>
<Reference ReferenceType="Organizes" IsForward="false">ns=1;s=::</Reference>
<Reference ReferenceType="Organizes">ns=1;s=::Program:lPflanze</Reference>
</References>
</UAObject>
<UAVariable DataType="i=4" ParentNodeId="ns=1;s=::Program" NodeId="ns=1;s=::Program:lPflanze" BrowseName="1:lPflanze" AccessLevel="3">
<DisplayName>lPflanze</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
</References>
<Extensions>
<Extension>
<pv:VariableExtension AuditEvents="true">
<ACL>
<ACE Role="1" Allow="0x017F"/>
<ACE Role="2" Allow="0x015F"/>
</ACL>
<Value>
<Binding Type="PV" Target="::Program:lPflanze"/>
</Value>
</pv:VariableExtension>
</Extension>
</Extensions>
</UAVariable>
</UANodeSet>
First Entry in XML is the Global declaration of Role and Allow
/UANodeSet/UAObject/Extensions/Extension/pv:ObjectExtension/ACL/ACE/@Role or @Allow
(without namespaces i know)
What i need is this in the end: The Datatype is Translated with lookup Table so unimportant for this issue.
Name: Datatype: Allow(Role):
gFahrzeug.Kraftfahrzeug.LKW[0] Byte 0x015F
gFahrzeug.Kraftfahrzeug.LKW[1] Byte 0x015F
gFahrzeug.Kraftfahrzeug.PKW String 0x015E
gLebewesen.Tier.Hund[0].Xoloitzcuintli[0] Boolean 0x015E
gLebewesen.Tier.Hund[0].Xoloitzcuintli[1] Boolean 0x011F
gLebewesen.Tier.Hund[0].MastinoNapoletano UInt32 0x015F
gLebewesen.Tier.Hund[1].Xoloitzcuintli UInt32 asdasda
Program:lPflanze Int16 asd
Or is there a better solution to get these Attributes and write them in different cells in Excel atm. im doing it with:
For Each xmlAttribute In xmlNodes
oTarget.Offset(iRow, 3).Value = xmlAttribute.Value
iRow = iRow + 1
Next xmlAttribute
回答1:
Ok someone, somehow, sometime is struggling with the same problem got it.
First of all if you want to set your XPath directory with variables working like this:
Set xmlNodesTempAllow = xmlDoc.SelectNodes("//*[@NodeId='" & ParentNodeId & "']//sonic:ACE[@Role=" & MyRoles & "]/@Allow")
Set xmlNodesTemp = xmlDoc.SelectNodes("//*[@NodeId='" & xmlNodes(i).Attributes(2).Text & "']//sonic:References/sonic:Reference[@ReferenceType='HasComponent']/@ReferenceType")
Set xmlNodesTemp = xmlDoc.SelectNodes("//*[@NodeId='" & xmlNodes(i).Attributes(2).Text & "']//sonic:Description") 'its not an Attribute
'Last one just with @Role - variable
Set xmlNodes = xmlDoc.SelectNodes("/sonic:UANodeSet/sonic:UAObject/sonic:Extensions/sonic:Extension/pv:ObjectExtension/sonic:ACL/sonic:ACE[@Role=" & MyRoles & "]/@Allow")
If ure done setting ure path an all is fine u can navigate inside xmlnodes like:
xmlNodesTemp(0).Text 'first entry of xmlnodes like arraycounting
xmlNodesTemp(0).NodeValue
xmlNodesTempParent(0).Attributes(1).Text 'first entry 2nd attribute
xmlNodesTempParent(0).ChildNodes(0).Attributes(1).Text 'first entry, first child, 2nd attribute...
if u want to know that ure XPatch found something u can check it like this
If xmlNodesTempAllow.Length > 0 Then
...
with these commands u can get all information from a xml u want.
来源:https://stackoverflow.com/questions/63015436/get-parent-attribute-inheritance-if-childnotes-missing-attribute