Getting specific elements with XPath from an SVG with Batik

自古美人都是妖i 提交于 2019-12-07 05:11:07

问题


I am trying to find some elements from an SVG document with Batik.

This is the example SVG document I am using:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<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">

  <g id="blubb">
   <line x1="0" y1="0" x2="40" y2="120" stroke="red" stroke-width="3"/>
   <text class="hurz" x="50" y="150">HURZ!</text>
  </g>
</svg>

I am trying to find all text elements with the class attribute hurz.

This is an example 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);
  }
}

When running this, the output is:

1
0
0

So obviously the XPath expression .//*[@class="hurz"] finds the correct nodes. However, when I search for specific element types (in this case text elements), it finds nothing as with the expressions .//text[@class="hurz"] and .//text.

Am I doing something wrong or is this a bug in Batik?


回答1:


This is because of the default namespace xmlns="http://www.w3.org/2000/svg". Try using the local-name() function:

.//*[local-name()=\"text\" and @class=\"hurz\"]


来源:https://stackoverflow.com/questions/26114760/getting-specific-elements-with-xpath-from-an-svg-with-batik

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!