【问题标题】:MarkLogic template driven extraction and triples: forming triples between array nodesMarkLogic 模板驱动提取和三元组:在数组节点之间形成三元组
【发布时间】:2017-11-27 14:42:48
【问题描述】:

这是我的问题的后续:MarkLogic template driven extraction and triples: dealing with array nodes

假设我有一些结构如下的文档:

declareUpdate();

xdmp.documentInsert(
       '/test/tde.json',
       {
         content: {
           name:'Joe Parent',
           children: [
             {
               name: 'Bob Child'
             },
             {
               name: 'Sue Child'
             },
             {
               name: 'Guy Child'
             }
           ]
         }
       },
       {permissions : xdmp.defaultPermissions(),
        collections : ['test']})

我想定义一个模板,该模板将从这些文档中提取三元组来定义子级之间的兄弟关系。对于上面的例子,我想提取以下三元组(关系是双向的):

Bob Child sibling-of Sue Child
Bob Child sibling-of Guy Child
Sue Child sibling-of Bob Child
Sue Child sibling-of Guy Child
Guy Child sibling-of Bob Child
Guy Child sibling-of Sue Child

如何设置我的模板来完成此操作?

谢谢!

【问题讨论】:

    标签: marklogic


    【解决方案1】:

    我不知道如何做到这一点,但似乎任何“兄弟姐妹”关系都等同于两个“孩子”关系。你能改成这样吗?

    {
      ?x is-parent-of ?a-child . 
      ?x is-parent-of ?b-child. 
      ?a-child != ?b-child
    }
    

    或者,如果您可以使用inference rules,您可以建立一个类似这样定义“兄弟姐妹”的规则。然后,尽管模板不直接生成“sibling-of”,但由于模板生成“is-parent-of”三元组,三元组仍然以与它们相同的方式绑定到摄取的文档。

    【讨论】:

      【解决方案2】:

      我将 JSON 转换为 XML,编写 XSLT 转换以获得 XML 三元组。如果需要,以后可以将 XML 转换回 JSON。

      XSLT 转换:

      <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://marklogic.com/xdmp/json/basic" xmlns:sem="http://marklogic.com/semantics" exclude-result-prefixes="json">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="json:content">
       <sem:triples xmlns:sem="http://marklogic.com/semantics">
        <xsl:apply-templates select="json:children"/>
       </sem:triples>
      </xsl:template>
      <xsl:template match="json:children">
       <xsl:for-each select="json:json">
        <xsl:variable name="subjectName">
         <xsl:value-of select="json:name/text()"/>
        </xsl:variable>
       <xsl:for-each select="following-sibling::* | preceding-sibling::* ">
        <sem:triple>
         <sem:subject>
          <xsl:value-of select="$subjectName"/>
         </sem:subject>
         <sem:predicate>sibling-of</sem:predicate>
         <sem:object><xsl:value-of select="json:name/text()"/></sem:object>
        </sem:triple>
      </xsl:for-each>
      </xsl:for-each>
      </xsl:template>
      </xsl:stylesheet>
      

      将 JSON 转换为 XML 并进行转换的 XQuery 代码:

      xquery version "1.0-ml";
      import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";
      declare namespace sem = "http://marklogic.com/semantics";
      declare variable $j := '{"content": {"name": "Joe Parent","children": [{"name": "Bob Child"}, {"name": "Sue Child"}, {"name": "Guy Child"}]}}';
      let $doc := json:transform-from-json($j),
      $xslt := doc("myTemplates.xsl"),
      $result :=  xdmp:xslt-eval($xslt,$doc),
      $config := json:config("custom"),
      $_ := map:put($config, "whitespace", "ignore"),
      $_ := map:put($config, "array-element-names", ("triple")),
      $_ := map:put($config, "element-namespace","http://marklogic.com/semantics")
      return json:transform-to-json($result, $config)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 2013-11-30
        • 2011-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多