简体   繁体   中英

lookup nodes with xpath in java

Can anybody suggest how can I solve this, there is XML document that I'm trying to append. I'm looking up nodes with xpath, the things is the software which generates this XML sometimes screws it up as following :

<element name="Element ">Element value</element>

So when I'm looking the node up with xpath using //element[@name="Element"] without a blank space I don't get a match. Naturally I get match with this //element[@name="Element "]

Is there something I can do to match this without blank space?Does xpath accept regular expressions or there is more smart way to do this, I can change the xml file as well after it has been generated by the sofware(with faulty user input).

(untested).

Would

//element[normalize-space(@name)=="Element"]

work?

A more general (though less accurate) solution could be:

//element[contains(@name,"Element")]

However that would also catch things like name="Elements" and name="Elementary" etc.

can you recommend some useful resources for xpath

Unfortunately there aren't many great such resources in my experience - w3schools is probably your best bet, or the spec itself for more advanced stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM