4

I have the below document. I am trying to get the test:Category at ABCSite[0]. Tried the XPath $ABCCustomerHierarchy(//ata:ABCSite[0])/test:Customer/test:CustomerID/test:Category, but I got some XPath exception.

Can anybody tell me the right XPath here please?

 <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>
carols10cents
  • 6,943
  • 7
  • 39
  • 56
Chris
  • 541
  • 1
  • 4
  • 11
  • Please, there is a preview _right below the edit box_. Your code formatting was horribly broken. Also, "some" error message does not help, show _the_ error message you received. – Jens Erat Nov 20 '13 at 23:11

2 Answers2

1

You can try :

//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • No . This is not getting me the node at [0] – Chris Nov 20 '13 at 23:09
  • 1
    XPath is 1-indexed. There is no element 0. If you want the first, @fastcodejava's solution is the correct one (yet he omitted the namespaces, this could be a problem). Try using _your_ query with namespaces, but index 1. – Jens Erat Nov 20 '13 at 23:14
  • I tried with namespace only, the issue is if i do comparison like //ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC' , 'ABC' is being compared against all the ABCSite at index [1] of its parent. so i tried $ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Cate‌ ​g‌ ​ory and got the exception : javax.xml.transform.TransformerException: Extra illegal tokens: – Chris Nov 20 '13 at 23:51
0

You should always specify the namespace (test for your example)

Another point is that arrays start with 1 not zero

If you want to get the value of that node, use text():

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

To match with exact value use:

//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category[text()='ABC_Customer']
evhen14
  • 1,839
  • 12
  • 16
  • I tried with namespace only, the issue is if i do comparison like //ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC' , 'ABC' is being compared against all the ABCSite at index [1] of its parent. so i tried $ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Categ‌ ​ory and got the exception : javax.xml.transform.TransformerException: Extra illegal tokens: – Chris Nov 20 '13 at 23:48
  • then take everything before [] into brackets. I edided my response – evhen14 Nov 20 '13 at 23:56
  • no worries. you should mark the answer as helpful to help others in future – evhen14 Nov 21 '13 at 21:17
  • Thanks evhen14. There is no error now. But (//test:ABCCustomerHierarchy/test:ABCSite[1])/test:Customer/test:CustomerID/test:Category = 'XYZ_Customer' return 'true' even if ABCSite[1] holds some other text as Category . Seems still it is checking the whole document. – Chris Nov 21 '13 at 21:20
  • So, you want it to return false if categories in CustomerReferences differ from XYZ_Customer. In other words, you want that all categories (in CustomerID and in CustomerReferences ) have XYZ_Customer? – evhen14 Nov 21 '13 at 21:24
  • No , I want to return true only if (//test:ABCCustomerHierarchy/test:ABCSite[1])/test:Customer/test:CustomerID/test‌​:Category = 'XYZ_Customer' . but its returning true even if (//test:ABCCustomerHierarchy/test:ABCSite[1])/test:Customer/test:CustomerID/test‌​:Category != 'XYZ_Customer' – Chris Nov 21 '13 at 23:43
  • i checked the online Xpath tester and this is working perfect there. Seems the issue is with the environment I am using . :( – Chris Nov 22 '13 at 00:31
  • I was using ABCSite[$counter] instead of ABCSite[1] where i had initialized counter to '1' . i tried with ABCSite[1] everything works perfect. Thanks a lot enhen14 ! – Chris Nov 22 '13 at 00:44