tags:

views:

313

answers:

1

I have such xml:

<A1>
  <B1>
    <C1>
    <C2>
    <C3>
  </B1>
  <B2>
    <C4>
    <C5>
    <C6>
  </B2>
</A1>
<A2>
  <B3>
    <C7>
    <C8>
    <C9>
  </B3>
  <B4>
    <C10>
    <C11>
    <C12>
  </B4>
</A2>

I need to transform it to table with nested rows:

<table border="yes">
<tr>
  <td>A1</td>
  <td>B1</td>
  <td>C1</td>
</tr>
<tr>
  <td></td>
  <td></td>
  <td>C2</td>
</tr>
<tr>
 <td></td>
 <td></td>
 <td>C3</td>
</tr>
<tr>
 <td></td>
 <td>B2</td>
 <td>C3</td>
</tr>
<tr>
 <td></td>
 <td></td>
 <td>C4</td>
</tr>

A and B appear only if they are new (not repeating in every row);

I'm trying to use position()

<xsl:template match="c">
 <tr>
  <td>
   <xsl:if test="IT IS THE FIRST C IN A">
     <xsl:value-of select="ancestor::A"/>
   </xsl:if>
  </td>
  <td>
   <xsl:if test="position(1)">
   <xsl:value-of select="parent"/>
   </xsl:if>
  </td>
  <td>
    <xsl:value-of select="."/>
  </td>
 </tr>
</xsl:template>

It seems that we should emulate position() for ancestor. Is there a general solution for any number of nested rows?

+2  A: 

You perhaps need something like the following (if I understood your question correctly):

<xsl:template match="C">
 <tr>
  <td>
   <xsl:if test="generate-id(ancestor::A/descendant::C[1]) = generate-id(.)">
     <xsl:value-of select="ancestor::A"/>
   </xsl:if>
  </td>
  <td>
   <xsl:if test="not(previous-sibling::C)">
     <xsl:value-of select=".."/>
   </xsl:if>
  </td>
  <td>
    <xsl:value-of select="."/>
  </td>
 </tr>
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates select="descendant::C"/>
</xsl:template>

Edit: you may also use

not(previous-sibling::C) and not(../previous-sibling::B)

as the first test (instead of using generate-id()).

fionbio
Many thanks, that works exactly:)
rudnev