-1

I am using Ecrion's Design Studio Publisher to design a template using a xml source. I am trying to achieve the following using xpath or xsl and am not sure how

For eg: i have the following xml file

<?xml version="1.0" encoding="utf-8"?>
<DOCUMENT>  
  <PLAN>
    <PLANNAME>Plan 1</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <CATEGORY ID="1">
                <LABEL>Insurance 1</LABEL>
                <PREMIUM>Premium 1</PREMIUM>
                <INNETWORK>In network 1</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <CATEGORY ID="2">
                <LABEL>Insurance 2</LABEL>
                <PREMIUM>Premium 4</PREMIUM>
                <INNETWORK>In network 4</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
  <PLAN>
    <PLANNAME>Plan 2</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <CATEGORY ID="1">
                <LABEL>Insurance 1</LABEL>
                <PREMIUM>Premium 2</PREMIUM>
                <INNETWORK>In network 2</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <CATEGORY ID="2">
                <LABEL>Insurance 2</LABEL>
                <PREMIUM>Premium 5</PREMIUM>
                <INNETWORK>In network 5</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
  <PLAN>
    <PLANNAME>Plan 3</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <CATEGORY ID="1">
                <LABEL>Insurance 1</LABEL>
                <PREMIUM>Premium 3</PREMIUM>
                <INNETWORK>In network 3</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <CATEGORY ID="2">
                <LABEL>Insurance 2</LABEL>
                <PREMIUM>Premium 6</PREMIUM>
                <INNETWORK>In network 6</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
  </DOCUMENT>

and i want to display the above in a table format like below

    <table style="width: 252px;" border="0" cellspacing="0" cellpadding="0"><colgroup><col span="3" width="84" /> </colgroup>
<tbody>
<tr>
<td class="xl65" style="text-align: center;" width="84" height="21">Plan 1</td>
<td class="xl65" style="text-align: center;" width="84">Plan 2</td>
<td class="xl65" style="text-align: center;" width="84">Plan 3</td>
</tr>
<tr>
<td class="xl66" style="text-align: center;" colspan="3" height="21">Insurance 1</td>
</tr>
<tr>
<td class="xl65" style="text-align: center;" height="21">Premium 1</td>
<td class="xl65" style="text-align: center;">Premium 2</td>
<td class="xl65" style="text-align: center;">Premium 3</td>
</tr>
<tr>
<td class="xl65" style="text-align: center;" height="21">In network 1</td>
<td class="xl65" style="text-align: center;">In network 2</td>
<td class="xl65" style="text-align: center;">In network 3</td>
</tr>
<tr>
<td class="xl66" style="text-align: center;" colspan="3" height="21">Insurance 2</td>
</tr>
<tr>
<td class="xl65" style="text-align: center;" height="21">Premium 4</td>
<td class="xl65" style="text-align: center;">Premium 5</td>
<td class="xl65" style="text-align: center;">Premium 6</td>
</tr>
<tr>
<td class="xl65" style="text-align: center;" height="21">In network 4</td>
<td class="xl65" style="text-align: center;">In network 5</td>
<td class="xl65" style="text-align: center;">In network 6</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>

If above table doesn't work, please apologize. It should look something like this

-------------------------------------------------
Plan 1       |       Plan 2       |       Plan 3
-------------------------------------------------
                  Insurance 1
-------------------------------------------------
Premium 1    |     Premium 2      |   Premium 3
-------------------------------------------------
In network 1 | In network 2       | In network 3
-------------------------------------------------
                  Insurance 2
-------------------------------------------------
Premium 4    |   Premium 5        |   Premium 6
-------------------------------------------------
In network 4 | In network 5       | In network 6
------------------------------------------------- 
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Andy
  • 15
  • 4
  • And yes, I don't want to hard code the Id values as they may vary file to file. – Andy Aug 13 '14 at 21:51
  • And what does the header look like if there are four or five or six insurance "plans" ... you have not provided all the information sufficient to make a generic solution. – Kevin Brown Aug 13 '14 at 22:45
  • 1
    Did you perhaps mistake "Xpath" for "XSLT"? Did you mean writing an XSLT stylesheet to accomplish this? – Mathias Müller Aug 14 '14 at 08:33
  • What have you tried, what was the result, and how did it differ from the result you wanted? The title says "repeat" but I don't see anything about what you want to repeat. – LarsH Aug 14 '14 at 10:49
  • I apologize if the question isn't self-explanatory. What I am trying to do here is to repeat the insurance section in a table (See above) and list the information under their respective plan names. I have not written any code as of now. But, Mathias is right. I meant XSLT – Andy Aug 14 '14 at 14:02
  • To answer Kevin, we can have maximum three plans only. So the table will look like that. It won't grow sideways but maybe longer, if there are more insurance companies. The fact that I didn't mention that in my question implies that I am not worried if there are more plans. – Andy Aug 14 '14 at 14:07
  • Please fix your input XML example, it's not well-formed: CATEGORY is not closed anywhere. -- Also please clarify if the CATEGORY ID is consistent across INSURANCEs; that is, does the n-th INSURANCE have one and the same ID under **any** PLAN. – michael.hor257k Aug 14 '14 at 15:10

1 Answers1

0

Given a well-formed input:

<DOCUMENT>  
  <PLAN>
    <PLANNAME>Plan 1</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <LABEL>Insurance 1</LABEL>
            <PREMIUM>Premium 1</PREMIUM>
            <INNETWORK>In network 1</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <LABEL>Insurance 2</LABEL>
            <PREMIUM>Premium 4</PREMIUM>
            <INNETWORK>In network 4</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
  <PLAN>
    <PLANNAME>Plan 2</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <LABEL>Insurance 1</LABEL>
            <PREMIUM>Premium 2</PREMIUM>
            <INNETWORK>In network 2</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <LABEL>Insurance 2</LABEL>
            <PREMIUM>Premium 5</PREMIUM>
            <INNETWORK>In network 5</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
  <PLAN>
    <PLANNAME>Plan 3</PLANNAME> 
    <BENEFITS>
        <INSURANCE>
            <LABEL>Insurance 1</LABEL>
            <PREMIUM>Premium 3</PREMIUM>
            <INNETWORK>In network 3</INNETWORK>
        </INSURANCE>    
        <INSURANCE>
            <LABEL>Insurance 2</LABEL>
            <PREMIUM>Premium 6</PREMIUM>
            <INNETWORK>In network 6</INNETWORK>
        </INSURANCE>
    </BENEFITS>
  </PLAN>  
</DOCUMENT>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:variable name="cols" select="DOCUMENT/PLAN" />
    <xsl:variable name="rows" select="DOCUMENT/PLAN[1]/BENEFITS/INSURANCE" />
    <table>
        <thead>
            <tr>    
                <xsl:for-each select="$cols">
                    <th><xsl:value-of select="PLANNAME"/></th>  
                </xsl:for-each>
            </tr>
        </thead>
        <tbody>
            <xsl:for-each select="$rows">   
                <xsl:variable name="rownum" select="position()" />
                <tr>
                    <th colspan="{count($cols)}"><xsl:value-of select="LABEL"/></th>    
                </tr>
                <tr>
                    <xsl:for-each select="$cols">   
                        <td><xsl:value-of select="BENEFITS/INSURANCE[$rownum]/PREMIUM"/></td>   
                    </xsl:for-each>
                </tr>
                <tr>
                    <xsl:for-each select="$cols">   
                        <td><xsl:value-of select="BENEFITS/INSURANCE[$rownum]/INNETWORK"/></td> 
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

</xsl:stylesheet>

will produce the following result:

<?xml version="1.0" encoding="UTF-8"?>
<table>
   <thead>
      <tr>
         <th>Plan 1</th>
         <th>Plan 2</th>
         <th>Plan 3</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <th colspan="3">Insurance 1</th>
      </tr>
      <tr>
         <td>Premium 1</td>
         <td>Premium 2</td>
         <td>Premium 3</td>
      </tr>
      <tr>
         <td>In network 1</td>
         <td>In network 2</td>
         <td>In network 3</td>
      </tr>
      <tr>
         <th colspan="3">Insurance 2</th>
      </tr>
      <tr>
         <td>Premium 4</td>
         <td>Premium 5</td>
         <td>Premium 6</td>
      </tr>
      <tr>
         <td>In network 4</td>
         <td>In network 5</td>
         <td>In network 6</td>
      </tr>
   </tbody>
</table>

rendered as:

enter image description here

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks Michael. I didn't get a chance to verify the code you sent but looks like it will work. I will let you know if I have any issues. Marking that as answer. Cheers – Andy Aug 20 '14 at 16:47