XML文档中索引节点的XPath

时间:2013-11-20 22:05:49

标签: xpath

我有以下文件。我想在test:Category处获得ABCSite[0]。 尝试了XPath $ABCCustomerHierarchy(//ata:ABCSite[0])/test:Customer/test:CustomerID/test:Category,但我得到了一些XPath异常。

有人能告诉我正确的XPath吗?

 <test:ABCCustomerHierarchy xmlns:test="http://wwwin.test.com/testschema">
        <test:ABCSite>
            <test:Customer>
                <test:CustomerID>
                    <test:ID>110984181</test:ID>
                    <test:Category>ABC_Customer</test:Category>
                </test:CustomerID>
            </test:Customer>
            <test:CustomerReferences>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>KLM_CUSTOMER</test:Category>
                </test:CustomerID>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>XYZ_Customer</test:Category>
                </test:CustomerID>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>PQR_CUSTOMER</test:Category>
                </test:CustomerID>
            </test:CustomerReferences>
        </test:ABCSite>
        <test:ABCSite>
            <test:Customer>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>XYZ_Customer</test:Category>
                </test:CustomerID>
            </test:Customer>
            <test:CustomerReferences>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>PQR_CUSTOMER</test:Category>
                </test:CustomerID>
            </test:CustomerReferences>
        </test:ABCSite>
    </test:ABCCustomerHierarchy>

2 个答案:

答案 0 :(得分:1)

您可以尝试:

//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category

答案 1 :(得分:0)

您应该始终指定命名空间(为您的示例测试)

另一点是数组从1开始而不是零

如果要获取该节点的值,请使用text():

//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category/text()

要与确切的值匹配使用:

//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category[text()='ABC_Customer']