2017-11-27 8 views
2

これは、ここで私の質問へのフォローアップです:MarkLogic template driven extraction and triples: dealing with array nodesMarkLogicテンプレート駆動型の抽出、トリプル:アレイのノード間で形成トリプル

それでは、私はこのような構造化文書の数を持っているとしましょう:

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 

:上記の例では、私は次のトリプルを(関係は双方向である)を抽出したいですか?

ありがとうございます!

答えて

0

JSONをXMLに変換し、XMLトリプルを取得するためのXSLT変換を作成しました。その後、必要に応じてXMLをJSONに変換することができます。

XSLT変換:XMLをJSONに変換し、変換を行い

<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> 

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) 
1

「私はそれを行う方法を知りませんが、それはどのように思えます兄弟関係は2つの「子関係」関係に相当します。代わりにこのようなことをすることができますか?

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

それともinference rulesを使用する位置にいる場合、あなたはそのような「兄弟-の」定義するルールを構築することができます。テンプレートは「兄弟」を直接生成しませんが、トリプルは「is-parent-of」トリプルを生成するテンプレートのおかげで、同じようにインジェストされたドキュメントに結び付けられます。

関連する問題