title: Getting specific elements with XPath from an SVG with Batik
body: <p>I am trying to find some elements from an SVG document with Batik.</p>
  
  <p>This is the example SVG document I am using:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  
  &lt;svg
     xmlns:svg="http://www.w3.org/2000/svg"
     xmlns="http://www.w3.org/2000/svg"
     version="1.0"     
     width="400"
     height="300"
     viewBox="0 -100 400 300"
     preserveAspectRatio="none"
     id="svg2"&gt;
  
    &lt;g id="blubb"&gt;
     &lt;line x1="0" y1="0" x2="40" y2="120" stroke="red" stroke-width="3"/&gt;
     &lt;text class="hurz" x="50" y="150"&gt;HURZ!&lt;/text&gt;
    &lt;/g&gt;
  &lt;/svg&gt;
  </code></pre>
  
  <p>I am trying to find all <code>text</code> elements with the class attribute <code>hurz</code>.</p>
  
  <p>This is an example code:</p>
  
  <pre><code>import java.awt.*;
  import java.io.*;
  import javax.swing.*;
  import org.apache.batik.swing.*;
  import org.apache.batik.swing.gvt.*;
  import org.w3c.dom.*;
  import org.w3c.dom.svg.*;
  import org.w3c.dom.xpath.*;
  
  public class XPathTest extends JFrame {
    private final JSVGCanvas c= new JSVGCanvas();
  
  
    public XPathTest(){
      this.setLayout(new GridLayout());
      this.add(c);
  
      c.setURI(new File("/tmp/bla.svg").toURI().toString());
  
      c.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
        @Override
        public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
          testXPath();
        }
      });
    }
  
    private void testXPath() {
      final SVGDocument document= c.getSVGDocument();
      final XPathEvaluator xpathEvaluator= (XPathEvaluator) document;
      final SVGElement searchRoot= (SVGElement) document.getElementById("blubb");
  
      //works
      final XPathResult result1= (XPathResult) xpathEvaluator.evaluate(".//*[@class=\"hurz\"]", searchRoot, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
      printResults(result1);
  
      //doesn't work                                                                                                                              
      final XPathResult result2= (XPathResult) xpathEvaluator.evaluate(".//text[@class=\"hurz\"]", searchRoot, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
      printResults(result2);
  
      //doesn't work
      final XPathResult result3= (XPathResult) xpathEvaluator.evaluate(".//text", searchRoot, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
      printResults(result3);
    }
  
    private void printResults(XPathResult result){
      int count= 0;
  
      Node node;
      while ((node= result.iterateNext()) != null) {
        count++;
      }
  
      System.out.println(count);
    }
  
    public static void main(String[] args){
      final XPathTest f= new XPathTest();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(800, 600);
      f.setVisible(true);
    }
  }
  </code></pre>
  
  <p>When running this, the output is:</p>
  
  <pre><code>1
  0
  0
  </code></pre>
  
  <p>So obviously the XPath expression <code>.//*[@class="hurz"]</code> finds the correct nodes. However, when I search for specific element types (in this case <code>text</code> elements), it finds nothing as with the expressions <code>.//text[@class="hurz"]</code> and <code>.//text</code>.</p>
  
  <p>Am I doing something wrong or is this a bug in Batik?</p>