简体   繁体   English

使用Batik从SVG中使用XPath获取特定元素

[英]Getting specific elements with XPath from an SVG with Batik

I am trying to find some elements from an SVG document with Batik. 我正在尝试通过Batik从SVG文档中找到一些元素。

This is the example SVG document I am using: 这是我正在使用的示例SVG文档:

<?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 . 我试图找到所有带有class属性hurz text元素。

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. 因此,显然XPath表达式.//*[@class="hurz"]找到了正确的节点。 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 . 但是,当我搜索特定的元素类型(在这种情况下为text元素)时,与表达式.//text[@class="hurz"].//text一样,找不到任何东西。

Am I doing something wrong or is this a bug in Batik? 我是在做错什么还是蜡染中的错误?

This is because of the default namespace xmlns="http://www.w3.org/2000/svg" . 这是因为默认名称空间xmlns="http://www.w3.org/2000/svg" Try using the local-name() function: 尝试使用local-name()函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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