1

I've the following dilemma: taken one XML like this:

<book id="$bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
</book>

I need to collect all attributes in the XML document beginning with "$" (and replace them with some data I have in memory). The hard part of it is that attributes with "$" can be on any Node of the Document, not just in the id Element like in the example. Is there any magic XPath expression that can make the trick?
Any other Java based solution is welcome.
Thanks in advance

user2824073
  • 2,407
  • 10
  • 39
  • 73

2 Answers2

1

Try //*/@*[starts-with(.,"$")]

http://www.freeformatter.com/xpath-tester.html#ad-output gives me Attribute='id="$bk102"' as XPath match for the XML example in the question.

(see https://stackoverflow.com/a/1390599/1712389 and https://stackoverflow.com/a/7405471/1712389)

Community
  • 1
  • 1
Rintze Zelle
  • 1,654
  • 1
  • 14
  • 30
1

An XPath expression as simple as

//@*[starts-with(.,'$')]

returns all attribute nodes whose value string starts with "$".

//                    anywhere in the document
@*                    an attribute with an arbitrary name
[                     but only if this attribute
starts-with(.,'$')]   has a value that starts with "$"
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75