简体   繁体   中英

Create XML nodes using XPath

Is it possible to create XML nodes using XPath ?

Assume i got the following XML:

<data>
    <someValue1></someValue2>
    <someValue2></someValue2>
    <someArray>
        <val></val>
        <val></val>
        <val></val>
    </someArray>
</data>

What I am trying to do is getting a node inside the XML using XPath (eg /data/someValue1 ) and setting the value of the node. This is of course easy to achieve with any language and framework supporting XML + XPath .

But when my XPath expression is pointing to a non existing node I want to somehow create this node. Even if the XPath expression is more complex ( eg /data/someArray/val[5] ).

At best this automatic node creation should be supported by some library. Is there an easy way for what I am trying to achieve? Currently I am using C++ with Qt and the QXmlQuery class.

Is it possible to create XML nodes using XPath?

No, it isn't. XPath can only select existing nodes.

If you want to create new nodes, you need XSLT or XQuery.

But even then, creating a node that satisfies a given path like /a/b/c/foo[5] is non-trivial, and not something that the languages can do automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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