【问题标题】:XPath Multiple Attribute Condition VariablesXPath 多属性条件变量
【发布时间】:2015-06-27 21:38:37
【问题描述】:

示例 XML

<catalog>
    <book id="Adventure" Type="Hardcover">
        <other>
            <author name="Jones" id="Adventure">
            </author>
        </other>
    </book>
</catalog>

这些说法有意义吗?我需要进行完整性检查以查看所有这些变量/引用的情况。我尝试使用其中一个 XPath 测试站点,但它一直给我一个错误,我不知道为什么。

bookVar = "Adventure"
bookStyle = "Hardcover"
bookAuthor = "Jon"

Set my_location = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """]/author/other/*[contains(name(),""" & bookAuthor & """]")

Set my_location2 = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """ and Type=""" & bookStyle &"""]

Set my_location3 = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """]/other/author/*[contains(name(),""" & bookAuthor & """]")

【问题讨论】:

    标签: xml vba dom xpath xml-parsing


    【解决方案1】:

    几个注意事项:

    • 在属性名称的开头使用@ 来引用xpath 中的XML 属性

    • 我建议在 xpath 中使用单引号,因为使用的字符串包装字符是双引号。这会让你的代码看起来更干净。

    • name()返回当前上下文元素/属性的名称,使用@name来引用名为name的XML属性

    • 您的某些路径与 XML 文档中元素的确切层次结构不对应

    xpath 查询示例:

    bookVar = "Adventure"
    bookStyle = "Hardcover"
    bookAuthor = "Jon"
    
    Set my_location = XMLFile.SelectNodes("/catalog/book[@id='" & bookVar & "']/other/author[contains(@name,'" & bookAuthor & "']")
    
    Set my_location2 = XMLFile.SelectNodes("/catalog/book[@id='" & bookVar & "' and @Type='" & bookStyle &"']")
    

    【讨论】:

    • 感谢您的评论,内容丰富且乐于助人!
    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多