XML文档中用于索引节点的XPath

4
我有以下的文件。我在尝试获取ABCSite[0]中的test:Category。 我尝试使用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>

请注意,在编辑框下方有一个预览窗口。您的代码格式已经严重损坏。此外,“某些”错误消息并没有什么帮助,请显示您收到的“具体”的错误消息。 - Jens Erat
2个回答

1
你可以尝试:

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

不行,这样无法获取[0]位置上的节点。 - Chris
1
XPath 是以 1 为起始索引的。没有元素 0。如果你想要第一个元素,@fastcodejava 的解决方案是正确的(尽管他省略了命名空间,这可能会成为问题)。试试使用 你的 带有命名空间的查询,但索引设置为 1。 - Jens Erat
我只尝试使用命名空间,但问题是如果我进行比较,例如//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC','ABC'将与其父级中索引为[1]的所有ABCSite进行比较。所以我尝试了$ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Cate‌​gory,但出现了异常:javax.xml.transform.TransformerException:Extra illegal tokens。 - Chris

0

你应该始终指定命名空间(例如,对于你的示例使用test)

另一个要点是数组从1开始而不是0

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

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

要匹配精确值,请使用:

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

我只尝试使用命名空间,但问题是如果我进行比较,例如//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC','ABC'将与其父级中索引为[1]的所有ABCSite进行比较。所以我尝试了$ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Categ‌​ory,但出现了异常:javax.xml.transform.TransformerException:Extra illegal tokens。 - Chris
然后将[]之前的所有内容放入括号中。我编辑了我的回复。 - evhen14
不用担心。您应该将答案标记为有帮助,以帮助其他人在未来。 - evhen14
谢谢evhen14。现在没有错误了。但是(//test:ABCCustomerHierarchy/test:ABCSite[1])/test:Customer/test:CustomerID/test:Category = 'XYZ_Customer'即使ABCSite[1]包含其他文本作为Category,也会返回'true'。似乎仍然在检查整个文档。 - Chris
所以,如果CustomerReferences中的类别与XYZ_Customer不同,您希望它返回false。换句话说,您希望所有类别(在CustomerID和CustomerReferences中)都是XYZ_Customer? - evhen14
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接