Sunday, 24 February 2019

Token a string with spaces into Nested-List


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vis="vishFunction"
    exclude-result-prefixes="xs vis"
    version="2.0" >
   
<xsl:variable name="assets" select="'123 234 456 789 0001'"/>
   
<xsl:output method="html"></xsl:output>
   
<xsl:template match="/">
       
<xsl:sequence select="vis:nestedList($assets)"/>
   
</xsl:template>
   
   
<xsl:function name="vis:nestedList">
       
<xsl:param name="assetID"/>
       
<ul>
           
<li>
               
<xsl:variable name="firstToken" select="tokenize(normalize-space($assetID),' ')[1]"/>
               
<xsl:value-of select="$firstToken"/>
               
<xsl:variable name="restAssets" select="normalize-space(substring-after($assetID,$firstToken))"/>
               
<xsl:if test="string-length(normalize-space($restAssets)) != 0">
                   
<xsl:message><xsl:value-of select="$restAssets"/></xsl:message>
               
<xsl:sequence select="vis:nestedList($restAssets)"/>
               
</xsl:if>
           
</li>
       
</ul>
   
</xsl:function>
</xsl:stylesheet>

Wednesday, 12 September 2018

Convert simple XML structure to Nested Structure

Input:-
<body>
    <p class="title">Article Title</p>
    <p class="Authors">abc, pqr and xyz</p>
    <p class="intro">here is introdution text......</p>        
    <p class="head1">1: Heading level 1</p>
    <p>some text here</p>
    <p>some text here</p>
    <p class="head2">1.1: Heading  level 2</p>
    <p>some text here</p>
    <p>some text here</p>        
    <p class="head3">1.1.1: Heading  level 3</p>
    <p>some text here</p>
    <p>some text here</p>        
    <p class="head1">2: Heading level 1</p>
    <p class="head2">2.1: Heading  level 2</p>
    <p>some text here</p>
    <p>some text here</p>        
    <p class="head3">2.1.1: Heading  level 3</p>
    <p>some text here</p>
    <p>some text here</p>        
    <p class="head3">2.1.2: Heading  level 3</p>
    <p>some text here</p>
    <p>some text here</p>        
</body>
Expected Output:
<?xml version="1.0" encoding="UTF-8"?> <body>    <section name="head1">       <title>1: Heading level 1</title>       <paras>          <p>some text here</p>          <p>some text here</p>       </paras>       <section name="head2">          <title>1.1: Heading  level 2</title>          <paras>             <p>some text here</p>             <p>some text here</p>          </paras>          <section name="head3">             <title>1.1.1: Heading  level 3</title>             <paras>                <p>some text here</p>                <p>some text here</p>             </paras>          </section>       </section>    </section>    <section name="head1">       <title>2: Heading level 1</title>       <section name="head2">          <title>2.1: Heading  level 2</title>          <paras>             <p>some text here</p>             <p>some text here</p>          </paras>          <section name="head3">             <title>2.1.1: Heading  level 3</title>             <paras>                <p>some text here</p>                <p>some text here</p>             </paras>          </section>          <section name="head3">             <title>2.1.2: Heading  level 3</title>             <paras>                <p>some text here</p>                <p>some text here</p>             </paras>          </section>       </section>    </section> </body>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vish="http://www.marklogicgd.blogspot.in/vish"     exclude-result-prefixes="xs vish" version="2.0">         <xsl:output indent="yes"/>         <xsl:template match="node()|@*">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>         <xsl:function name="vish:group" as="element(section)*">         <xsl:param name="entries" as="element(p)*"/>         <xsl:param name="level" as="xs:integer"/>         <xsl:for-each-group select="$entries"             group-starting-with="p[@class = concat('head',$level)]">             <xsl:variable name="P_ID" select="generate-id(.)"/>             <section name="{@class}">                 <title>                     <xsl:value-of select="."/>                 </title>                 <xsl:if test="following-sibling::p[1][not(@class)]">                     <paras>                         <xsl:apply-templates                             select="following-sibling::p[not(@class)][generate-id(preceding-sibling::p[@class][1]) = $P_ID]"                         />                     </paras>                 </xsl:if>                                 <xsl:sequence select="vish:group(current-group() except ., ($level + 1))"/>             </section>         </xsl:for-each-group>     </xsl:function>         <xsl:template match="body">         <xsl:copy>             <xsl:sequence select="vish:group(p[contains(@class,'head')], 1)"/>         </xsl:copy>     </xsl:template> </xsl:stylesheet>