Home > Enterprise >  Merge 'p' element content under the following 'p element' content
Merge 'p' element content under the following 'p element' content

Time:11-20

It is list content, right now bullet list and item content appearing separately in the output. So I want to merge a <p>, Please look expected output and help me on that. Thanks in advance

Input xml:

<kk>
    <group class="li">
        <line>
            <t style="li"></t>
        </line>
    </group>
    <group class="p">
        <line>
            <t style="p">content here</t>
        </line>
        <line>
            <t style="p">content here1</t>
        </line>
        <line>
            <t style="p">content here2</t>
        </line>
    </group>    
</kk>

Current Output:

<kk>
    <group class="li">
        <line>
            <t style="li">• content here content here1 content here2</t>
        </line>
    </group>
</kk>

Current XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    
</xsl:stylesheet>

Expected output:

<kk>
<p>• content here</p>
</kk>

CodePudding user response:

Here is an example using for-each-group group-starting-with to merge any "group" of elements starting with that bullet:

  <xsl:template match="kk">
    <xsl:copy>
      <xsl:for-each-group select="group" group-starting-with="group[@class = 'li' and ki/p[@class = 'li'] = '•']">
        <p>
          <xsl:value-of select="current-group()//p"/>
        </p>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

CodePudding user response:

Kita Ansari, if you need to place all nested p elements following code should help:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="kk group line"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="kk">
    <xsl:copy>
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:copy>
</xsl:template>

<xsl:template match="group">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="line">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="t">
    <xsl:value-of select="concat(., ' ')"/>
</xsl:template>
</xsl:stylesheet>

My local output with provided Input xml:

<kk>
  <p>• content here content here1 content here2 </p>
</kk>

Please let me know if it works for you.

  • Related