0

I have a payload like mentioned below.

<A xmlns:com="http://www.example.com/xml/xmlns/example">
    <B>
        <X appCode="001">xxx</X>
        <Y appId="0002">yyy</Y>
    </B>
    <C>
        <D>
            <E>
                <F id="1" code="001">test-1</F>
                <F id="3" code="002">test-2</F>
                <F id="4" code="003">test-3</F>
                <F id="5" code="004">test-4</F>
            </E>
        </D>
    </C>
</A>

I need to get the id value for A/C/D/E/F['test-2']. Also, I have to use local-name() to avoid namespaces. Tried the following and get the below result.

//* [local-name() = 'C']/* [local-name() = 'D']/* [local-name() = 'E']/* [local-name() = 'F']

<F id="1" code="001">test-1</F>
<F id="3" code="002">test-2</F>
<F id="4" code="003">test-3</F>
<F id="5" code="004">test-4</F>

How to find the id value of 'test-2' which is 3? (NOTE: I'm using this in wso2 EI property mediator)

Thank You in Advance!

koko
  • 169
  • 1
  • 13
  • Does this answer your question? [Getting attribute using XPath](https://stackoverflow.com/questions/4531995/getting-attribute-using-xpath) – Thomas Hansen Apr 07 '21 at 16:26
  • I have gone through this post before. It's not what I'm searching for. I needed to get the attribute value for a given element value. – koko Apr 07 '21 at 16:33

1 Answers1

2

Well, given your example, the expression:

//*[local-name() = 'F'][. = 'test-2']/@id

will return 3.

However, it needs to be said that namespaces are to be used, not avoided. Declare the namespace in your stylesheet, assign it a prefix, and use the prefix to address the elements.

Note also that in the given example, the namespace declaration is entirely redundant. You could use simply:

"//F[. = 'test-2']/@id"

or a bit more efficiently:

"/A/C/D/E/F[. = 'test-2']/@id"
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • This is exactly what I needed. Regarding namespaces, it can vary in the payload, so I need to disregard it. Thanks a lot for your support!! – koko Apr 07 '21 at 16:07