简体   繁体   English

使用XPath创建XML节点

[英]Create XML nodes using XPath

Is it possible to create XML nodes using XPath ? 是否可以使用XPath创建XML节点?

Assume i got the following XML: 假设我得到以下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. 我想要做的是使用XPath (例如/data/someValue1 )在XML内获取一个节点并设置该节点的值。 This is of course easy to achieve with any language and framework supporting XML + XPath . 使用支持XML + XPath任何语言和框架,这当然很容易实现。

But when my XPath expression is pointing to a non existing node I want to somehow create this node. 但是,当我的XPath表达式指向不存在的节点时,我想以某种方式创建此节点。 Even if the XPath expression is more complex ( eg /data/someArray/val[5] ). 即使XPath表达式更复杂( 例如 /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. 目前,我正在将C ++QtQXmlQuery类一起使用。

Is it possible to create XML nodes using XPath? 是否可以使用XPath创建XML节点?

No, it isn't. 不,不是。 XPath can only select existing nodes. XPath只能选择现有节点。

If you want to create new nodes, you need XSLT or XQuery. 如果要创建新节点,则需要XSLT或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. 但是即使那样,创建一个满足给定路径的节点(如/a/b/c/foo[5] )也不是一件容易的事,这并不是语言可以自动完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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