Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I want to transform one xml file in to another xml file by using xslt 2.0 transform.
I have struck on making some recursive calls.

This is XML File

XML
<pre lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text1-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading2" />
        </w:pPr>
        <w:r>
            <w:t>Text2-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text3-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text4-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading2" />
        </w:pPr>
        <w:r>
            <w:t>Text5-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text6-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading3" />
        </w:pPr>
        <w:r>
            <w:t>Text7-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text8-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading1" />
        </w:pPr>
        <w:r>
            <w:t>Text9-</w:t>
        </w:r>
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text10-</w:t>
        </w:r>
    </w:p>


</w:body>
</w:document>




XSL FILE is

XML
<pre lang="xml"><xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs w mf">

<xsl:output indent="yes"/>

<xsl:function name="mf:group" as="element()*">
  <xsl:param name="paragraphs" as="element()*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$paragraphs" group-starting-with="p[pPr/pStyle/@w:val = concat('Heading', $level)]">
    <xsl:choose>
      <xsl:when test="self::p[pPr/pStyle/@w:val = concat('Heading', $level)]">
        <xsl:element name="Heading{$level}">
          <Title><xsl:value-of select="r/t"/></Title>
          <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="current-group()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:function>

<xsl:template match="document">
  <Document>
    <xsl:sequence select="mf:group(body/p, 1)"/>
  </Document>
</xsl:template>

<xsl:template match="p">
  <Paragraph>
    <xsl:value-of select="r/t"/>
  </Paragraph>
</xsl:template>

</xsl:stylesheet>




My Required Output is :

XML
<document>
   <paragraph>Text1-</paragraph>
   <Heading2>
      <Title>Text2-</Title>
      <paragraph>Text3-</paragraph>
      <paragraph>Text4-</paragraph>
    </Heading2>
      <Heading2>
         <Title>Text5-</Title>
         <paragraph>Text6-</paragraph>
         <Heading3>
            <Title>Text7-</Title>
            <paragraph>Text8-</paragraph>
         </Heading3>
      </Heading2>
   <Heading1>
      <Title>Text9-</Title>
      <paragraph>Text10-</paragraph>
   </Heading1>
</document>


Please Guide me to get out of this issue...
Posted
Updated 27-Jun-12 19:34pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 2:29am    
What do you call "a call"? XSLT is a purely declarative language...
--SA
saravanan6 28-Jun-12 2:32am    
@Sergey Alexandrovich : I Agree. I have to written some recursive function.So, I have struck on that...

1 solution

Interesting example, but the approach should be declarative.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900