Project

Profile

Help

saxon 9.0.0.4 + XPath 2.0 fn:remove()+Java

Added by Anonymous about 16 years ago

Legacy ID: #4949100 Legacy Poster: lopatnic marian (shiocaltz)

hy ppl, i am a student, just started to study the Saxon framework by developing a project which i will have to turn in at the end of the semester. the project is about CRUD operations on XML files. I've managed to read data from my XML file using FLOWR expressions, but when i try to remove some data from it, nothing happens :(. Here is my code: package storage.logic; import java.io.File; import java.util.Iterator; import net.sf.saxon.s9api.DocumentBuilder; import net.sf.saxon.s9api.Processor; import net.sf.saxon.s9api.QName; import net.sf.saxon.s9api.WhitespaceStrippingPolicy; import net.sf.saxon.s9api.XPathCompiler; import net.sf.saxon.s9api.XPathSelector; import net.sf.saxon.s9api.XdmAtomicValue; import net.sf.saxon.s9api.XdmItem; import net.sf.saxon.s9api.XdmNode; import net.sf.saxon.s9api.XdmSequenceIterator; import net.sf.saxon.s9api.XdmValue; public class Post { public static void main(String[] args) { try { Processor proc = new Processor(false); XPathCompiler xpe = proc.newXPathCompiler(); // Compile the XPath expressions used by the application DocumentBuilder builder = proc.newDocumentBuilder(); builder.setWhitespaceStrippingPolicy(WhitespaceStrippingPolicy.ALL); XdmNode bowStorage = builder .build(new File("G:/work/eclipse 3.2 workspace/BuildingsOnWeb/src/storage/data/bow.xml")); XdmSequenceIterator iterator = bowStorage.iterator(); while (iterator.hasNext()) { System.out.println(bowStorage.iterator().next()); } QName itemList = new QName("", "", "itemList"); QName position = new QName("", "", "position"); xpe.declareVariable(position); xpe.declareVariable(itemList); XPathSelector removeItem = xpe.compile("remove($itemList,$position)").load(); removeItem.setContextItem(bowStorage); removeItem.setVariable(itemList, bowStorage); removeItem.setVariable(position, (XdmValue) new XdmAtomicValue(3)); System.out .println("----------->>>>>>>>>before the remove <<<<<<<<<<<---------"); Iterator<XdmItem> removeIterator = removeItem.iterator(); while (removeIterator.hasNext()) { System.out.println(removeIterator.next()); } XdmValue result = removeItem.evaluate(); System.out .println("----------->>>>>>>>>after the remove <<<<<<<<<<<---------"); Iterator<XdmItem> resultIterator = result.iterator(); while (resultIterator.hasNext()) { System.out.println(resultIterator.next()); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished."); } } and this is my XML file: <?xml version="1.0" encoding="UTF-8"?> <bow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bow.xsd"> <cladire id="1" nume="C" proprietar="Alexandru I Cuza" tip="vechi"> <sala id="403"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>35</calculatoare> </sala> <sala id="405"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>30</calculatoare> </sala> <sala id="409"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>30</calculatoare> </sala> <sala id="411"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>32</calculatoare> </sala> <sala id="412"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>31</calculatoare> </sala> <sala id="413"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>35</calculatoare> </sala> <sala id="201"> <facultate>Informatica</facultate> <etaj>1</etaj> <calculatoare>35</calculatoare> </sala> <sala id="904"> <facultate>Informatica</facultate> <etaj>9</etaj> <calculatoare>5</calculatoare> </sala> </cladire> </bow> i have read that in XMD everything is a sequence so when i try to remove the element on position 3 of the sequence, the <sala id="405"> <facultate>Informatica</facultate> <etaj>2</etaj> <calculatoare>30</calculatoare> </sala> should be removed. What am i doing wrong? P.s. I get sometimes: net.sf.saxon.trans.XPathException: Finding root of tree: the context item is undefined Thank you for your time, and sorry about my huge post


Replies (1)

RE: saxon 9.0.0.4 + XPath 2.0 fn:remove()+Jav - Added by Anonymous about 16 years ago

Legacy ID: #4972735 Legacy Poster: Michael Kay (mhkay)

Sorry for the delay in responding, for some reason this dropped off my TODO list. When you do this XdmNode bowStorage = builder .build(new File("G:/work/eclipse 3.2 workspace/BuildingsOnWeb/src/storage/data/bow.xml")); you are defining bowStorage as a sequence of length one, whose value is a document node, the root of the XML tree. When you remove the item at position 3 in a sequence of length one, the sequence is returned unchanged. Michael Kay http://www.saxonica.com/

    (1-1/1)

    Please register to reply