使用Batik从SVG获取具有XPath的特定元素 [英] Getting specific elements with XPath from an SVG with Batik

查看:226
本文介绍了使用Batik从SVG获取具有XPath的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Batik从SVG文档中找到一些元素。

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

这是我正在使用的示例SVG文档:

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>

我正在尝试查找所有文本元素使用class属性 hurz

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

这是一个示例代码:

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);
  }
}

运行时,输出为:

1
0
0

显然XPath表达式 .//* [@ class =hurz] 找到正确的节点。但是,当我搜索特定的元素类型(在这种情况下是 text 元素)时,它找不到表达式 .// text [@class =hurz] .// text

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.

我在做什么错误或者这是Batik中的错误吗?

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

推荐答案

这是因为默认命名空间 xmlns = http://www.w3.org/2000/svg。尝试使用 local-name()函数:

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

这篇关于使用Batik从SVG获取具有XPath的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆