Replace div tag with p tag only if it does not have direct childs as p/ol/ul tags

Viewed 28

I need to replace div tag with p tag only if div tag doesn't contains direct child as p/ol/ul tag and if it contains p/ol/ul tag as direct child then just remove the div tag and keep the child tags as it is.

Example:

 <div>
      <ul>
         <li>HIPAA Privacy Module Certificate</li>
      </ul>
  </div>
 <div>
      <p>
         HIPAA Privacy Module Certificate
      </p>
  </div>
 <div>
      <strong>
         <li>HIPAA Privacy Module Certificate</li>
      </strong>
  </div>

Desired output:

  <ul>
      <li>HIPAA Privacy Module Certificate</li>
  </ul>
  <p>
     HIPAA Privacy Module Certificate
  </p>
 <p>
      <strong>
         <li>HIPAA Privacy Module Certificate</li>
      </strong>
  </p>

What i'm trying but didn't work:

    <xsl:template match="div[not(div)]">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
 <xsl:template match="ul[(parent::div)]">
            <ul>
                <xsl:apply-templates/>
            </ul>

    </xsl:template>


<xsl:template match="ol[(parent::div)]">
                <ol>
                    <xsl:apply-templates/>
                </ol>
    
        </xsl:template>
 <xsl:template match="p[(parent::div)]">
                <p>
                    <xsl:apply-templates/>
                </p>
    
        </xsl:template>

Thanks in advance.

1 Answers

To me it sounds like doing

<xsl:template match="div[p | ol | ul]">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="div[not(p | ol | ul)]">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

together with the identity transformation.

Related