【问题标题】:XPath query for a node节点的 XPath 查询
【发布时间】:2011-04-14 05:31:49
【问题描述】:

我应该做错了什么。显示名称下所有名为“名称”的节点的 XPath 查询是什么?我正在使用 libxml2。

<?xml version="1.0" encoding="UTF-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006" xmlns:d="http://openoffice.org/extensions/description/2006"  xmlns:xlink="http://www.w3.org/1999/xlink">
    <version value="2010.05.25" />
    <identifier value="German.frami2006DE.dictionary.from.org.openoffice.de.by.Karl.Zeiler" />
    <display-name>
        <name lang="en">German (DE-frami) spelling, hyphenation, thesaurus</name>
        <name lang="de">Deutsche (DE-frami) Rechtschreibung, Trennung, Thesaurus</name>
    </display-name>
    <platform value="all" />
    <dependencies>
        <OpenOffice.org-minimal-version value="3.0" d:name="OpenOffice.org 3.0" />
    </dependencies>
</description>

【问题讨论】:

    标签: xml xpath libxml2


    【解决方案1】:

    您的文档有一个默认命名空间,因此您需要在表达式中注册该命名空间。我没用过libxml2,但是看他们的examples,看来你要的功能是xmlXPathRegisterNs

    请注意,当您注册命名空间时,您为 XPath 表达式选择的前缀不必与文档中使用的前缀(如果有)匹配。由于您的文档使用默认命名空间(不是前缀),因此肯定不会。但是,您仍然需要在 XPath 中使用前缀:

    /ns:description/ns:display-name/ns:name

    【讨论】:

      【解决方案2】:

      以下是有效的:

      xmlXPathRegisterNs(Ctxt, 'ns1', 'http://openoffice.org/extensions/description/2006');
      Res := xmlXPathEval('//ns1:name', Ctxt);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多