-4

I have an xml like;

<a>
<b att="x">
</b>
</a>

I parse this xml with java and i have a node b in my hand.My question is how can i convert this node to a xpath expression a/b[@att='x'] ?

Qwerky
  • 18,217
  • 6
  • 44
  • 80
user1665039
  • 17
  • 1
  • 7
  • 1
    what is your goal ? `a/b[@att='x']` looks like the right expression for getting the tag named b – nl-x Sep 13 '13 at 08:19
  • finding a node from another document with same attributes. – user1665039 Sep 13 '13 at 08:20
  • 2
    You can't convert a node to an xpath expression. That doesn't make sense. You can *evaluate* an xpath expression against a node, and even get a node in response. – Qwerky Sep 13 '13 at 08:20
  • `a/b[@att='x']` should do it. Show us your code if it's not working for you. – nl-x Sep 13 '13 at 08:21
  • is there another way to find a node from another document with same attributes? – user1665039 Sep 13 '13 at 08:21
  • @user1665039 please tell us step by step what you mean. and give examples of what it should do. I now think you want an xpath expressions to return another xpath expression. But I'm not sure on how exactly it should behave. Is it always the `` tag you're looking for, but the att is always different ? – nl-x Sep 13 '13 at 08:23
  • Are looking for [this (Get Xpath from the org.w3c.dom.Node)][1] [1]: http://stackoverflow.com/questions/5046174/get-xpath-from-the-org-w3c-dom-node – stan Sep 13 '13 at 08:25
  • i have a document node and i want to find a node from another document with the same attributes with this node.Isn't taht clear? – user1665039 Sep 13 '13 at 08:26
  • 1
    No, it's not clear. Is it always the node named `b` ? What if a AND b both have attributes? Then what ? Or what if neither? Or what if there is a node `c` inside `b`? Or if there are 2 `b` nodes ? Right now, you're question is too localized. Try showing us your code. – nl-x Sep 13 '13 at 08:27
  • It's not remotely clear. You need to post your code and explain clearly in a step by step manner what you want to achieve, and where the issue in your code arises. What if there's a hundred b nodes, and some are null, and some have b1 inside them, and others have different fomatting? – RossC Sep 13 '13 at 08:42
  • there is hundred b nodes but have diffrent attribute values(no null and no b1 inside them.) – user1665039 Sep 13 '13 at 08:52
  • @user1665039 You should also show the data you are working with. Your explanation is quite ambiguous. As others already indicated, no one will be able to help you with the currently available information you have given. – dirkk Sep 13 '13 at 09:53

1 Answers1

1

I don't think there is an absolutely generic solution for this. In xpath 2.0 you could probably make something like

string-join(
    for $x in ./ancestor-or-self::* return 
        if ($x/@att) 
            then concat($x/name(), '[@att="', $x/@att, '"]') 
            else $x/name()
    , '/'
)

For input

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>
        <b att="x"></b>
        <b att="y"></b>
        <b att="z"></b>
    </a>
</root>

supposing you are holding 3rd b element it gives string

root/a/b[@att="z"]

But it's very limited in this form (it look only for one attribute, this attribute has to be named "att", it doesn't work with position in case of multiple elements b with same @att value, etc. etc.) and has to be perhaps written in more precise form. It is just an idea.

Jirka Š.
  • 3,388
  • 2
  • 15
  • 17