XPath搜索不起作用

时间:2013-09-27 13:52:59

标签: xml dom xpath dom4j xbrl

我尝试使用dom4j访问xml文件。我也试过通过org.w3c的dom,但这也以同样的方式失败了。 我有一个xml文件的示例,我尝试在下面阅读。我尝试从元素loc读取属性xlink:href,但由于某种原因,这总是失败。当我在一个简单的xml文件上尝试相同的方法时,我自己写,它确实有效。我已经做了好几天了。这是我的方法:

 File file = new File("schemas/pfs-2013-04-01-presentation.xml");
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);
            XPath xpath = document.createXPath("//loc");
            Map uris = new HashMap();
            uris.put("", "http://www.xbrl.org/2003/linkbase");
            uris.put("xbrli","http://www.xbrl.org/2003/instance");
            uris.put("xlink","http://www.w3.org/1999/xlink");
            uris.put("xsi","http://www.w3.org/2001/XMLSchema-instance");

            xpath.setNamespaceURIs(uris);

            List<Node> nodes =  xpath.selectNodes(xpath);

从这些'节点'我想稍后阅读这些属性。当我执行此时,List是空的。这是不明白的事情。

有人可以帮我吗? 感谢

<?xml version="1.0" encoding="UTF-8"?>
<linkbase xmlns="http://www.xbrl.org/2003/linkbase" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd" xmlns:presentationAttribute="http://www.nbb.be/be/fr/pfs/presentationAttribute" >
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullIdentifyingData" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullIdentifyingData"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullBalanceSheet" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullBalanceSheet"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullIncomeStatement" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullIncomeStatement"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullAppropriationsWithdrawings" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullAppropriationsWithdrawings"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullDisclosures" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullDisclosures"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullSocialBalanceSheet" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullSocialBalanceSheet"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullValidationRules" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullValidationRules"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullManagementReport" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullManagementReport"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullAccountantsReport" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullAccountantsReport"/>
<presentationLink xlink:type="extended" xlink:role="http://www.nbb.be/be/fr/pfs/ci/role/FullIdentifyingData">
    <loc xlink:type="locator" xlink:href="pfs-2013-04-01.xsd#pfs_IdentifyingData" xlink:label="IdentifyingData_1" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_GlobalCommonDocument" xlink:label="GlobalCommonDocument_2" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityInformation" xlink:label="EntityInformation_3" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityName" xlink:label="EntityName_4" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityCurrentLegalName" xlink:label="EntityCurrentLegalName_5" />

1 个答案:

答案 0 :(得分:0)

由于您没有使用默认命名空间,因此xpath不知道如何找到“loc”节点。您需要为“loc”节点添加前缀,并更新要用于相同前缀的正确名称空间。有关详情,请参阅此处:XPath and Default Namespace handling