如何从具有命名空间的XML中获取节点

时间:2020-07-20 10:00:59

标签: xml vba xpath

使用命名空间获取xml。问题是我没有得到任何节点。 (带*的字符除外)搜索有关正确语法的多个站点和帖子……无济于事。低于我的xml和我在vba中的代码。这些命名空间plz帮助我在做什么错。通过// @ NodeId我可以得到我想要的属性,但这还不够,我需要Node ....我什至没有得到第一个(UANodeSet)xmlNodes说= Nothing ....任何帮助都很好

    xmlDoc.Load Application.GetOpenFilename()
    xmlDoc.setProperty "SelectionLanguage", "XPath"
    
    Application.ScreenUpdating = False
    oTarget.Parent.EnableCalculation = False
    
    XmlNamespaces = "xmlns:ua='http://xxx/NodeSet.xsd'"
    xmlDoc.setProperty "SelectionNamespaces", XmlNamespaces
    Set xmlNodes = xmlDoc.SelectNodes("/ua:UANodeSet/ua:UAVariable)

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="xxx/PV.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd">
    <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>
</UANodeSet>

1 个答案:

答案 0 :(得分:1)

您的主要问题是没有注册默认名称空间。您的XML包含多个带前缀的命名空间,一个不带前缀的命名空间:

带前缀:

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="xxx/PV.xsd" 

无前缀:

xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"      

不带前缀的名称空间被认为是默认名称空间,如果节点不保留前缀,则文档中的所有基础节点都将属于该名称空间。由于<UANodeSet><UAVariable>没有冒号分隔的前缀(与<pv:VariableExtension>不同),因此必须在VBA中此默认名称空间下解析其内容。

以下使用用户定义的名称sonic注册所有名称空间和默认名称空间,该名称将在XPath解析中使用。注意:名称空间以空格分隔的字符串组合。

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:ua='http://xxx/NodeSet.xsd' " & _ 
                "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " & _ 
                "xmlns:pv='xxx/PV.xsd'" 

xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.setProperty "SelectionNamespaces", XmlNamespaces

Set xmlNodes = xmlDoc.SelectNodes("/sonic:UANodeSet/sonic:UAVariable")
相关问题