It is real scenario only I have changed the data. I have below parent child relationship xml. I am trying to convert the below xml using XSLT.I am able to trverse the parent data but not able to give the conditions for child node like if child reference change for any moment it should seperate the sample otherwise it should seperate by childs.
Input document
<Samples>
<Sample>
<a1>a1name</a1>
<b1>b1desc</b1>
<c1ref>101</c1ref>
<childref>101</childref>
<eno>test</eno>
<ename>somename</ename>
</Sample>
<Sample>
<a1>a1name</a1>
<b1>b1desc</b1>
<c1ref>101</c1ref>
<childref>101</childref>
<eno>test123</eno>
<ename>someothername</ename>
</Sample>
<Sample>
<a1>a1name1</a1>
<b1>b1desc1</b1>
<c1ref>102</c1ref>
<childref>102</childref>
<eno>test1234</eno>
<ename>someothername1</ename>
</Sample>
<Sample>
<a1>a1name</a1>
<b1>b1desc</b1>
<c1ref>101</c1ref>
<childref>101</childref>
<eno>test</eno>
<ename>somename</ename>
</Sample>
<Sample>
<a1>a1name1</a1>
<b1>b1desc1</b1>
<c1ref>103</c1ref>
<childref>103</childref>
<eno>test1234</eno>
<ename>someothername1</ename>
</Sample>
</Samples>
Something the OP hasn't explained. Possibly an expected output document
<Samples>
<Sample>
<a1>a1name</a1>
<b1>b1desc</b1>
<c1ref>101</c1ref>
<childs>
<childref>101</childref>
<eno>test</eno>
<ename>somename</ename>
</childs>
<childs>
<childref>101</childref>
<eno>test123</eno>
<ename>someothername</ename>
</childs>
</Sample>
<Sample>
<a1>a1name1</a1>
<b1>b1desc1</b1>
<c1ref>102</c1ref>
<childs>
<childref>102</childref>
<eno>test1234</eno>
<ename>someothername1</ename>
</childs>
</Sample>
</Samples>
The below XSLT work but it again repeats the childref 101.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="Samples">
<xsl:copy>
<!-- select the first Sample -->
<xsl:apply-templates select="Sample[1]"/>
</xsl:copy> </xsl:template>
<xsl:template match="Sample">
<!-- the a1 attribute in Sample will act as the identifier
(check if it is the same element) -->
<xsl:variable name="identifier" select="a1"/>
<xsl:copy>
<xsl:apply-templates select="a1"/>
<xsl:apply-templates select="b1"/>
<xsl:apply-templates select="c1ref"/>
<xsl:element name="childs">
<xsl:apply-templates select="childref"/>
<xsl:apply-templates select="eno"/>
<xsl:apply-templates select="ename"/>
</xsl:element>
<!-- get childs of Sample with same identifier -->
<xsl:apply-templates
select="following-sibling::Sample[a1=$identifier]"
mode="SameElement"/>
</xsl:copy>
<!-- select the nex Samples with different identifier -->
<xsl:apply-templates select="following-sibling::Sample[a1!=$identifier][1]"/> </xsl:template>
<xsl:template match="Sample" mode="SameElement">
<!-- here only output the child elements -->
<xsl:element name="childs">
<xsl:apply-templates select="childref"/>
<xsl:apply-templates select="eno"/>
<xsl:apply-templates select="ename"/>
</xsl:element> </xsl:template>
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
How can I write the xslt which will produce the above output?