XPath::compile & XPathExpression::evaluate
Added by Anonymous over 16 years ago
Legacy ID: #5187031 Legacy Poster: David (pagod)
Hi, I want to write an XSLT extension function in Java using the Saxon API, and before I start to do that I wanted to experiment a bit with Saxon's data structures. I've been playing with this XPathExample.java provided in the samples with Saxon 9.0.0.4J, and tried it on this othello.xml file. It works fine, no problem. Then after I had stripped it a bit I wanted to try it on a file of mine. I've modified only the go function, it now looks like this: public void go(String filename) throws Exception { System.setProperty( "javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); XPath xpe = xpf.newXPath(); InputSource is = new InputSource(new File(filename).toURL().toString()); SAXSource ss = new SAXSource(is); NodeInfo doc = ((XPathEvaluator)xpe).setSource(ss); xpe.setXPathVariableResolver(this); XPathExpression findLine = xpe.compile("//LINE"); List matchedLines = (List)findLine.evaluate(doc, XPathConstants.NODESET); TinyNodeImplelement; inti = 0; for( Iterator< TinyNodeImpl > it = matchedLines.iterator() ; it.hasNext() ; i++ ) { element = (net.sf.saxon.tinytree.TinyNodeImpl)( it.next() ); System.err.println( i + ": " + element.getDisplayName() ); } } not much left, and it still works fine on othello.xml -- it displays 3556 times "LINE", which is OK. now I've tried this code on a file of mine. it's a small svg file: <?xml version="1.0"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="453.55pt" height="310.75pt" viewBox="1134 1418 15695 10754 "> <g id="_x0000_s1053"> <g id="_x0000_s1059"> <svg x="5682" y="5038" width="636" height="484" viewBox="0 0 21600 21600" preserveAspectRatio="none" fill="white" stroke="cyan" stroke-width="1000pt" opacity="1" overflow="visible"> <path d="M 10800,0 L 0,10800 10800,21600 21600,10800 z "/> </svg> </g> <g transform=" translate(12012 0) matrix(-1 0 0 1 0 0) rotate(90 6006 5921)" id="_x0000_s1073"> <svg x="5606" y="5917" width="800" height="9" viewBox="0 0 21600 21600" preserveAspectRatio="none" fill="none" stroke="purple" stroke-width="1000pt" opacity="1" overflow="visible"> <path d="M 0,0 L 0,0 0,21600 21600,21600 "/> </svg> </g> <g id="_x0000_s1104"> <svg x="5224" y="6321" width="1569" height="485" viewBox="0 0 21600 21600" preserveAspectRatio="none" fill="white" stroke="red" stroke-width="1000pt" opacity="1" overflow="visible"> <path d="M 0,0 L 0,21600 l 21600,0 L 21600,0 z "/> </svg> </g> </g> </svg> and I've changed the XPath expression "//LINE" into "//g". and well, i don't get any results! :-o if i use "//" as the expression, i get all the nodes in the tree, but "/svg", "//svg", "/" and so on don't work either. what am i doing wrong??? i can't see any difference between my file and othello.xml (besides the obvious), and i have absolutely no clue why my XPath expressions (well, most of them at least) don't work, while obviously it has managed to read and parse the file. it'd be real cool if someone could try it and tell me what they get -- or simply explain what stupid mistake i may have made here! i'm running the Java program in eclipse with java 1.5.0_05. Thanks a lot! David
Replies (2)
RE: XPath::compile & XPathExpression::evaluate - Added by Anonymous over 16 years ago
Legacy ID: #5187132 Legacy Poster: muriloq (muriloq)
Perhaps it's a problem with the namespace ( "http://www.w3.org/2000/svg" ), are you declaring and using it correctly ? After declaring the namespace as, for example, svg, you shoud try //svg:svg or //svg:path ...
RE: XPath::compile & XPathExpression::evaluat - Added by Anonymous over 16 years ago
Legacy ID: #5191155 Legacy Poster: Michael Kay (mhkay)
I'm afraid this is one of the most basic beginner's mistakes in XPath. //g is selecting elements named g in no namespace, whereas you want elements named g in namespace "http://www.w3.org/2000/svg". Best to declare a prefix bound to this namespace and select using this prefix, or alternatively you could make it the default namespace for elements. Michael Kay Saxonica
Please register to reply