开发者

XQuery: Inserting Nodes

I'm reading in an XML file using XQuery and want to insert several nodes/elements and generate a new XML file. How can I accomplish this?

I've tried using the replace() function, but, it开发者_StackOverflow社区 looks like all my XML tags are being stripped when I call doc() to load my document. So calling replace() isn't any good if my XML tags are being removed.

Any help? Are there other technologies I can use?


An extension to the XQuery language allowing updates -- the XQuery Update Facility -- exists to allow documents to be modified.

Inserting a node looks like this:

insert node <foo>bar</foo>
  into /bar//baz[id='qux']

Among other engines, this is supported by BaseX.

See http://www.w3.org/TR/xquery-update-10/


replace() is a string operation, so the XML will be converted to a string before replacement.

To create a modified copy of the original file, you can modify an identity transformation which recursively copies the original file to insert the new nodes where required - see the article in the XQuery Wikibook

Alternatively if the file is in an XML database such as eXist, you can use update operations to insert elements in situ.


Using XQuery Scripting you can write programs like this:

variable $stores := doc("stores.xml")/stores;

insert node element store { 
  element store-number { 4 },
  element state { "CA" }
} into $stores;

$stores

You can try such example live at http://www.zorba-xquery.com/html/demo#vpshT+pVURyQSCEOKrFBrF0jyGY=

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜