Question

I´ve an xml instance similar to this:

<outerElementList>
   <outerElement>
       <outerID>1</outerID>
       <outerName>test1</outerName>
       <innerElementList>
           <innerElement>
               <innerID>10</innerID>
               <innerName>test10</innerName>
           </innerElement>
           <innerElement>
               <innerID>20</innerID>
               <innerName>test20</innerName>
           </innerElement>
       </innerElementList>
   </outerElement>
   <outerElement>
       <outerID>2</outerID>
       <outerName>test2</outerName>
       <innerElementList>
           <innerElement>
               <innerID>30</innerID>
               <innerName>test30</innerName>
           </innerElement>
           <innerElement>
               <innerID>40</innerID>
               <innerName>test40</innerName>
           </innerElement>
       </innerElementList>
   </outerElement>

And I need to end up with something like this:

<ElementList>
   <Element>
       <ID>1</ID>
       <Name>test1</Name>          
   </Element>
   <Element>
       <ID>2</ID>
       <Name>test2</Name>              
   </Element>
   <Element>
       <ID>10</ID>
        <SuperID>1</SuperID>
       <Name>test10</Name>         
   </Element>
   <Element>
       <ID>20</ID>
       <SuperID>1</SuperID>            
       <Name>test20</Name> 
   </Element>
   <Element>
       <ID>30</ID> 
       <SuperID>2</SuperID>
       <Name>test30</Name> 
   </Element>
   <Element>
       <ID>40</ID> 
       <SuperID>2</SuperID>
       <Name>test40</Name> 
   </Element>

SuperID of an inner element being the ID og the enclosing outer element. I realize that I need to use the looping functoid, but I cant seem to get i right.

This question is a spinoff off 22035260 basvo provided a great answer to the original question, where both the inner and outer structure only consisted of a single ID. Unfortunately it isnt easily applied to a more complex structure.

Was it helpful?

Solution

What you will probably need to do is create an intermediate schema with two record types and two maps. The first map is shown below.

Intermediate map

This results in the below XML.

<ElementList>
  <ElementRoot>
    <ID>1</ID>
    <Name>test1</Name>
  </ElementRoot>
  <ElementRoot>
    <ID>2</ID>
    <Name>test2</Name>
  </ElementRoot>
  <Element>
    <ID>10</ID>
    <SuperID>1</SuperID>
    <Name>test10</Name>
  </Element>
  <Element>
    <ID>20</ID>
    <SuperID>1</SuperID>
    <Name>test20</Name>
  </Element>
  <Element>
    <ID>30</ID>
    <SuperID>2</SuperID>
    <Name>test30</Name>
  </Element>
  <Element>
    <ID>40</ID>
    <SuperID>2</SuperID>
    <Name>test40</Name>
  </Element>
</ElementList>

Then this is easily mapped

Second Map

Resulting in

<ElementList>
  <Element>
    <ID>1</ID>
    <Name>test1</Name>
  </Element>
  <Element>
    <ID>2</ID>
    <Name>test2</Name>
  </Element>
  <Element>
    <ID>10</ID>
    <SuperID>1</SuperID>
    <Name>test10</Name>
  </Element>
  <Element>
    <ID>20</ID>
    <SuperID>1</SuperID>
    <Name>test20</Name>
  </Element>
  <Element>
    <ID>30</ID>
    <SuperID>2</SuperID>
    <Name>test30</Name>
  </Element>
  <Element>
    <ID>40</ID>
    <SuperID>2</SuperID>
    <Name>test40</Name>
  </Element>
</ElementList>

Note: The other option would of course be to do it via Custom XSLT.

OTHER TIPS

In addition to Dijkgraafs very competent answer, this is how we solved the problem using XSLT.

<ftns17:outerElementList xmlns:ftns17="http://[Parent-Scheme]" xmlns:ftns19="http://[Element-Scheme]">
  <xsl:for-each select="/*[local-name() = 'Parent']/*[local-name() = 'outerElementList'][1]/*[local-name() = 'outerElement']">
    <xsl:variable name="id" select="*[local-name() = 'ID']/text()" />
    <ftns17:outerElement>
      <ftns19:ID>
        <xsl:value-of select="$id" />
      </ftns19:ID>
      <ftns19:Name>
        <xsl:value-of select="*[local-name() = 'Name']/text()" />
      </ftns19:Name>
    </ftns17:outerElement>
    <xsl:for-each select="*[local-name() = 'innerElementList']/*[local-name() = 'innerElement']">
      <ftns17:innerElement>
        <ftns19:ID>
          <xsl:value-of select="*[local-name() = 'ID']/text()" />
        </ftns19:ID>
        <ftns19:SuperID>
          <xsl:value-of select="$id" />
        </ftns19:SuperID>
        <ftns19:Name>
          <xsl:value-of select="*[local-name() = 'Name']/text()" />
        </ftns19:Name>      
    </ftns17:innerElement>    
  </xsl:for-each>  
</xsl:for-each>
</ftns17:outerElementList>

Using inline XSLT in a scripting functoid and attaching the output to the elementlist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top