Project

Profile

Help

how can i modify a node's text value using saxon's xpath 2.0 expression?

Added by bruce oy over 11 years ago

after a lot of googling, i failed to modify node's text value or attribute value with saxon's xpath 2.0 expression.

for example, i have a string, its content is valid soap message. i compiled a xpath 2.0 expression like '@/:envelope/:body/:a/:b/*:c/@', and try to modify the node's text value, but i can not find the way. also can not find any sample code.

saxon version: saxon he 9.2 for java. appreciate for any comments.


Replies (6)

Please register to reply

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by Michael Kay over 11 years ago

The default tree model in Saxon, the TinyTree, is read-only: this gives substantial performance benefits in normal XSLT and XQuery processing.

If you require in-situ update of a tree, I would recommend either (a) using XQuery update, or (b) if you want to do the modification from Java code, using a tree model such as JDOM2. This is a lot slower than the TinyTree, partly because it is mutable.

The alternative, of course, is rather than modifying the tree in place, to do an XSLT transformation to create a new tree.

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by bruce oy over 11 years ago

some information i provided before should be corrected. i am using saxon he 9.4.0.6 for java, not 9.2

i tried modify text content using saxon with jdom but failed. the test code follows: @ XPathFactory xpf = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpe = xpf.newXPath();

    // Build the source document.
    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

    dfactory.setNamespaceAware(true);
    long start = System.nanoTime();
    DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
    final StringReader characterStream = new StringReader(xmlStr);
    Node doc = docBuilder.parse(new InputSource(characterStream));
    XPathExpression exp = xpe.compile("/*:Envelope/*:Body/*:qqCheckOnline/*:qqCode");
    NodeList matchedLines = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    for (i = 0; i < matchedLines.getLength(); i++)
    {
        Node n = matchedLines.item(i);
        n.setNodeValue("1111aaaa");

    }
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(buffer));
    String str = buffer.toString();
    System.out.println(str);
    // Create a reader for reading input from the console@

taking your advice, i try to modify the code to using jdom2 api, but failed. there is no corresponding api for jdom2 such as net.sf.saxon.option.jdom.DocumentWrapper

can you provide some sample codes? thanks a lot for your great work.

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by Michael Kay over 11 years ago

Sorry, I misled you. JDOM2 support is implemented but won't be released until 9.5 comes out. For the moment it has to be JDOM (1) (or XOM, etc).

In fact your original code wasn't using the TinyTree, it was using DOM. (Shame you didn't post it earlier...)

You haven't actually said how it is failing: what are the symptoms? If you can show enough code (just enough) to reproduce the problem, that will be very helpful.

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by bruce oy over 11 years ago

in the previously posted codes, i try to modify node's text content : @n.setNodeValue("1111aaaa")@, and then i expect @doc@ object been modified because of calling @setNodeValue@ method.but @doc@'s content remained untouched. my original goal is : i have a soap message string, i use saxon's xpath expression to modify it and get a new soap message string. i still have some questions:

before saxon 9.5 being released, is there a development version of saxon 9.5? if does, how can i get it ?

when saxon 9.5 will be released? is there a roadmap?

how can i achieve my goal using saxon with jdom or xom or dom4j or any other object model? if can, can you provide some sample code?

thanks a lot !

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by Michael Kay over 11 years ago

This should work with any object model (except JDOM2 which isnt' released until 9.5). JDOM2 won't solve your problem.

You are doing something wrong, and I don't know what, because you haven't shown enough of your code.

It's also nothing to do with Saxon, as far as I can see. You're finding a node using a Saxon XPath expression, and I assume from what you have said that you are finding the node successfully. After that, as far as I can see, Saxon is out of the loop, and it's only your own code that needs to be examined.

RE: how can i modify a node's text value using saxon's xpath 2.0 expression? - Added by bruce oy over 11 years ago

my goal has bean achieved. thanks for your help!

    (1-6/6)

    Please register to reply