【问题标题】:Specific Attribute node replacement特定属性节点替换
【发布时间】:2012-12-06 12:41:12
【问题描述】:

如何在 MarkLogic 中为特定属性进行节点替换?例如如下:

<chapters>
<title id="primary">first primary content</title>
<title id="primary">second primary content</title>
<title id="secondary">this is amy middle content</title>
<title id="terciary">this is amy last content</title>
</chapters>

我想要如下:

<chapters>
<title id="primary">third primary content</title>
<title id="secondary">this is amy middle content</title>
<title id="terciary">this is amy last content</title>
</chapters>

我的意思是假设 A.xml 文件存储在 MarkLogic 数据库服务器中,其中包含类似 bleow 的数据:

<chaptermetadata>
<chapters>
<title id="primary">first content</title>
<title id="primary">second content</title>
<title id="secondary">This is middle content</title>
<title id="terciary">This is last content</title>
</chapters>
<chapters>
<title id="primary">fouth content</title>
<title id="primary">fifth content</title>
<title id="primary">sixth content</title>
<title id="secondary">This is new content</title>
<title id="terciary">This is old content</title>
</chapters>
<chaptermetadata>

现在,我想在所有章节中替换所有元素 title 中的一个节点,其中包含属性 @id='primary',如下所示:

<chaptermetadata>
<chapters>
<title id="primary">common content</title>
<title id="secondary">This is middle content</title>
<title id="terciary">This is last content</title>
</chapters>
<chapters>
<title id="primary">common content</title>
<title id="secondary">This is new content</title>
<title id="terciary">This is old content</title>
</chapters>
<chaptermetadata> 

【问题讨论】:

  • 鉴于两个代码块之间的差异,很难理解您要求进行何种转换

标签: xquery marklogic


【解决方案1】:

如果您刚刚开始使用 XQuery 和 MarkLogic,http://developer.marklogic.com/learn/technical-overviewhttp://developer.marklogic.com/learn 可能会有所帮助。

修改元素和属性的最佳方式取决于您未提供的上下文。我想第一个问题是“如何按属性选择节点?”一个简单的 XPath 就可以做到这一点。对于数据库中的所有章节:

/chapters/title[@id eq $id]

...或相对于先前选择的元素序列(章节)*

$chapters/title[@id eq $id]

如果这是一个数据库文档,您可以使用 http://docs.marklogic.com/xdmp:node-replacehttp://docs.marklogic.com/xdmp:node-delete 函数从那里获取它。如果节点仅在内存中,请参阅http://docs.marklogic.com/guide/app-dev/typeswitch 以获取有关使用 XQuery 类型开关或 XSLT 的指导和示例。在http://developer.marklogic.com/blog/tired-of-typeswitch 有更多关于typeswitch 和XSLT 的示例和比较。

【讨论】:

    【解决方案2】:

    根据您提供的示例,您似乎希望将text() 替换为具有@id='primary', and remove the othertitleelements with@id='primary'`的第一个title 元素。

    下面将使用xdmp:node-replace()xdmp:node-delete() 方法实现这一点。

    for $primary-title in doc("A.xml")/chaptermetadata/chapters/title[@id='primary']
    return 
      if ($primary-title/preceding-sibling::title[@id='primary']) 
      then xdmp:node-delete($primary-title)
      else xdmp:node-replace($primary-title/text(), text{"common content"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 2015-06-12
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多