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>

Monday, 5 August 2013

XSLT 2 Find Occurence of each distinct character in a string

<?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" exclude-result-prefixes="xs" version="2.0">
   
    <!--Two ways two find the occurrence of distnict character in a string-->
   
    <xsl:template match="/">
        <xsl:variable name="value" select="'Vishnu singh shekhawat'"/>
        <xsl:variable name="EachChar">
        <xsl:analyze-string select="." regex="\c">
            <xsl:matching-substring>
                <a><xsl:value-of select="."/></a>
            </xsl:matching-substring>
        </xsl:analyze-string>
        </xsl:variable>
        <xsl:for-each select="distinct-values($EachChar/a)">
            <xsl:sort select="." case-order="lower-first" data-type="text"/>
            <xsl:value-of select="."/> is <xsl:value-of select="count(tokenize($value,.)) - 1"/> <xsl:text> times &#x0A;</xsl:text>   
        </xsl:for-each>
    </xsl:template>
   
   
    <xsl:template match="/">
        <xsl:variable name="value" select="translate('Vishnu singh shekhawat',' ','')"/>
        <xsl:variable name="codePoints" select="string-to-codepoints($value)"/>
        <xsl:variable name="DistnictValue" select="distinct-values($codePoints)"/>
        <xsl:for-each select="$DistnictValue">
            <xsl:value-of select="codepoints-to-string(.)"/> is <xsl:value-of select="string-length($value) - string-length(translate($value,codepoints-to-string(.),''))"/><xsl:value-of select="'&#x0A;'"></xsl:value-of>
        </xsl:for-each>
    </xsl:template>
   
</xsl:stylesheet>