I'm having the input xml like below:
<sect1>
<title>Intro</title>
<sect2>
<title>Purpose</title>
<sect3>
<title>Scope</title>
</sect3>
</sect2>
<sect2>
<title>Take</title>
<table><title>Table 1</title></table>
</sect2>
</sect1>
I have created the XSL for the above xml:
<xsl:variable name="xmlpath" select="/files/path"/>
<xsl:variable name="rootLangLoc" select="/files/@xml:lang"/>
<xsl:variable name="newline"><xsl:text> </xsl:text></xsl:variable>
<xsl:variable name="linebreak"><xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:text>Top Heading,Sub Heading</xsl:text>
<xsl:value-of select="$linebreak"/>
<xsl:for-each select="files/file">
<xsl:variable name="FullName" select="concat($xmlpath, ., $rootLangLoc, '.xml')"/>
<xsl:apply-templates select="document($FullName)" mode="csvprocess"/>
</xsl:for-each>
<xsl:for-each select="/sect1/title">
<xsl:apply-templates select="."/>
<xsl:for-each select="/sect1/sect2/title">
<xsl:value-of select="$newline"/>
<xsl:text>,</xsl:text>
<xsl:apply-templates select="/sect1/sect2/title"/>
<xsl:for-each select="/sect1/sect2/sect3/title">
<xsl:value-of select="$newline"/>
<xsl:text>,</xsl:text>
<xsl:text>,</xsl:text>
<xsl:apply-templates select="/sect1/sect2/sect3/title"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
I got the CSV output like:
Intro
,Purpose
,Take
,,Scope
Excepted output would be:
Intro
,Purpose
,,Scope
,Take
I want all the section title is to be in proper order as per the input xml. I'm getting the output like section 1 title's first and section 2 title's next as like.
If I am guessing correctly the logic that you're trying to apply here, you could do simply:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:text>Top Heading,Sub Heading </xsl:text>
<xsl:for-each select="//title ">
<xsl:value-of select="for $i in 1 to count(ancestor::*) - 1 return ','" separator=""/>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/bwdwrS
Another way you could use to get the level of the current title
is:
substring-after(name(..), 'sect')