简体   繁体   English

如何使用XPath查询在xml节点(空节点/附加到现有内容)中添加字符串内容

[英]How to add string content in xml nodes(empty node/append to existing content) using XPath Query

I have the following xml structure already available with the childnodes as title, desc, symp, diag, treat addinfo etc. I want to check that whether addinfo contains any thing and also append some string to it like "This is additional info". 我已经有以下xml结构,其子节点已经可以用作标题,desc,symp,diag,对待addinfo等。我想检查addinfo是否包含任何东西,并向其附加一些字符串,例如“ This is Additional info”。 There are many disease title I need to check the addifo tag according to disease title-title tag. 我需要根据疾病标题-标题标签检查addifo标签的疾病标题很多。

<chapter>
  <disease type="Name">
    <title>Name</title>
    <desc>--------------</desc>
    <symp>--------------</symptoms>
    <diag>-------------</diagnosis>
    <treat>-------------</treatment>
    <addinfo></addinfo>
  </disease>
</chapter

> >

I am using XPath query for searching the content of the tags according to the disease name. 我正在使用XPath查询根据疾病名称搜索标签的内容。 Thanks 谢谢

You can use an XPath like this to see if addinfo is empty or not: //addinfo[not(node())] 您可以使用这样的XPath来查看addinfo是否为空: //addinfo[not(node())]

UPDATE: 更新:

Ok, now am totally lost as to what you want. 好吧,现在完全失去了想要的东西。 It sounds like a really straightforward problem. 听起来像是一个非常简单的问题。 You would create some sort of getText() method that given an XML node, gets the text associated with it (if any or returns null or empty string). 您将创建某种getText()方法,该方法给定一个XML节点,获取与之关联的文本(如果有的话,或者返回null或空字符串)。 Then you would do a traversal of all the diseases and for each disease, perform your logic: if( string.isNullOrEmpty(getText(disease, "addinfo")) ) { // do something } -- if you want to check whether addinfo is empty or add stuff to it etc.. 然后,您将遍历所有疾病,并针对每种疾病执行逻辑: if( string.isNullOrEmpty(getText(disease, "addinfo")) ) { // do something } -如果您要检查addinfo为空或向其中添加内容等。

XPath can only retrieve information from your document, it cannot modify it. XPath只能从您的文档中检索信息,而不能对其进行修改。

XQuery can produce a modified copy of your document, but it's not particularly easy, because you need to explicitly copy all the parts that you don't want to change. XQuery可以生成文档的修改后的副本,但这并不是特别容易,因为您需要显式地复制所有您不想更改的部分。

XSLT is a better bet for producing a modified copy of the document; XSLT是产生文档修改副本的更好选择。 or XQuery Update Facility if you prefer. 或根据需要选择XQuery更新工具。

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

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