Ashish Bhardwaj
Ashish Bhardwaj

Reputation: 81

MarkLogic TDE xpath on multiple repeating fields

I need to create template using TDE. Eventually i want to be able to view data in using my view. The problem i see is because of multiple repeating elements in my xml. Can anyone please help me on how to create such a template. Here is the sample of the xml:

<entity>
  <id>id-2</id>
  <name>pub-2</name>
  <series name="s-21">s-21</series>
  <series name="s-22">s-22</series>  
  <series name="s-23">s-23</series>  
  <location>loc-1</location>
  <location>loc-2</location>    
</entity>

I want to see 6 rows for the combination of both the repeatable fields i.e. series and location in this case. Any help will be highly appreciable.

Upvotes: 2

Views: 206

Answers (1)

Ashish Bhardwaj
Ashish Bhardwaj

Reputation: 81

Found a way using nested templates. Here is an example template:

<template xmlns="http://marklogic.com/xdmp/tde">
  <context>entity</context>
  <rows>
    <row>
      <schema-name>main</schema-name>
      <view-name>root</view-name>
      <view-layout>sparse</view-layout>
      <columns>
        <column>
          <name>id</name>
          <scalar-type>string</scalar-type>
          <val>id</val>
        </column>
        <column>
          <name>name</name>
          <scalar-type>string</scalar-type>
          <val>name</val>
        </column>        
    </columns>
    </row>
  </rows>

  <templates>
    <template>
      <context>series</context>
      <rows>
        <row>
          <schema-name>main</schema-name>
          <view-name>series</view-name>
          <view-layout>sparse</view-layout>
          <columns>
            <column>
              <name>idParent</name>
              <scalar-type>string</scalar-type>
              <val>../id</val>              
            </column>

            <column>
              <name>series</name>
              <scalar-type>string</scalar-type>
              <val>fn:string(.)</val>
              <nullable>true</nullable>
            </column>
          </columns>
        </row>
      </rows>
    </template>
    <template>
      <context>location</context>
      <rows>
        <row>
          <schema-name>main</schema-name>
          <view-name>location</view-name>
          <view-layout>sparse</view-layout>
          <columns> 
            <column>
              <name>idParentlocation</name>
              <scalar-type>string</scalar-type>
              <val>../id</val>              
            </column>
            <column>
              <name>name</name>
              <scalar-type>string</scalar-type>
              <val>fn:string(.)</val>
              <nullable>true</nullable>
            </column>
          </columns>
        </row>
      </rows>
    </template>
  </templates>  
</template>

Here is the sql query:

select * from main.root 
join main.location on main.location.idParentlocation = main.root.id
join main.series on main.series.idParent = main.root.id

Upvotes: 3

Related Questions