Search code examples
xpathemma

XPath for overview values of emma report


I generate EMMA reports during my build, they look like this

<report>
  <stats>
    <packages value="110"/>
    <classes value="1762"/>
    <methods value="12617"/>
    <srcfiles value="962"/>
    <srclines value="61320"/>
  </stats>
  <data>
    <all name="all classes">
      <coverage type="class, %" value="2%   (42/1762)"/>
      <coverage type="method, %" value="2%   (302/12617)"/>
      <coverage type="block, %" value="3%   (6849/258033)"/>
      <coverage type="line, %" value="3%   (1592.9/61320)"/>

I need to get the percentage from the value attributes of the /report/data/all/coverage nodes. Currently getting these:

Invalid XPath expression: "/report/data/all/coverage[starts-with(@type,'block')]@value": Unexpected '@'
Invalid XPath expression: "substring-before(/report/data/all/coverage[starts-with(@type,'block']@value,'%')": Expected: )

Solution

  • You are missing a forward slash from the XPath before the @value attribute. There is also a missing brackets in your starts-with function

    Try the following

    /report/data/all/coverage[starts-with(@type,'block')]/@value
    
    
    substring-before(/report/data/all/coverage[starts-with(@type,'block')]/@value,'%')