XML文档中索引节点的XPath

前端之家收集整理的这篇文章主要介绍了XML文档中索引节点的XPath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下文件.我正在尝试进行测试: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>

解决方法

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

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

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

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

要与精确值匹配使用:

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

猜你在找的XML相关文章