Reputation: 347
I want to order up the Topic elements by using the XSL. I'm having lot of segment, that all segment I had taken as separate topic. Now I want get the different Input, So i want to change like below
My Input XML is:
<Segments>
<Segment>
<Name>Food poisoning?</Name>
<Body>
<p>Food</p>
</Body>
</Segment>
<Segment>
<Name>Causes food poisoning?</Name>
<Body>
<p>Most</p>
<h3>Salmonella</h3>
<p>most</p>
</Body>
</Segment>
</Segments>
XSL I used as:
<xsl:template match="Segments">
<xsl:for-each-group select="*" group-starting-with="Segments">
<topic outputclass="">
<xsl:attribute name="id">topic_Head_<xsl:number count="Segments | Segment"/></xsl:attribute>
<title/>
<xsl:for-each-group select="current-group()" group-starting-with="Segment">
<xsl:choose>
<xsl:when test="self::Segment">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="Segment"/></xsl:attribute>
<xsl:apply-templates select="node()"/>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="Name">
<title><xsl:apply-templates/></title>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="h3">
</xsl:template>
<xsl:template match="Body">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
Output I'm getting as:
<topic outputclass="" id="topic_Head_1">
<title/>
<topic id="topic_1">
<title>Food poisoning?</title>
<body>
<p>Food</p>
</body>
</topic>
<topic id="topic_2">
<title>Causes food poisoning?</title>
<body>
<p>Most</p>
<p>most</p>
</body>
</topic>
</topic>
Expected Output be like:
<topic outputclass="" id="topic_Head_1">
<title/>
<topic id="topic_1">
<title>Food poisoning?</title>
<body>
<p>Food</p>
</body>
</topic>
<topic id="topic_2">
<title>Causes food poisoning?</title>
<body>
<p>Most</p>
</body>
<topic id="topic_3">
<title>Salmonella</title>
<body>
<p>most</p>
</body>
</topic>
</topic>
</topic>
I Need the h3 tag has to be come like another sub-topic inside the segment. Please provide your suggestions. Thanks in advance
Upvotes: 0
Views: 241
Reputation: 25936
My idea is: for the Body element thake 2 different paths whether or not h3 occurs. No h3 path is trivial. For h3 path:
Here is the code
<xsl:template match="Body">
<xsl:choose>
<xsl:when test="h3">
<body>
<xsl:apply-templates select="h3[1]/preceding-sibling::*"/>
</body>
<xsl:for-each-group select="h3[1] | h3[1]/following-sibling::*" group-starting-with="h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="Segment | h3" level="any"/></xsl:attribute>
<title><xsl:value-of select="." /></title>
<xsl:apply-templates select="current-group()"/>
</topic>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<body>
<xsl:apply-templates select="node()"/>
</body>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Upvotes: 1