vendredi 10 février 2017

I need to realign the element tag using XSLT

Vote count: 0

I'm having multiple topics, I need to include section tag along with the Topic element with numbering ID's

My Input XML is:

<Article>
<Title>Mam</Title>
<Items>
<Item>
<Name>cyst1?</Name>
<Body>
<h1>aaa</h1>
<p>knee1.</p>
<h2>bbb</h2>
<p>knee2.</p>
</Body>
</Item>
<Item>
<Name>cyst2?</Name>
<Body>
<p>knee3.</p>
</Body>
</Item>
</Items>
</Article>

XSL I Used as:

<xsl:stylesheet version="2.0" xmlns:xsl="http://ift.tt/tCZ8VR" exclude-result-prefixes="#all">

  <xsl:template match="Article">
 <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="Items">
 <xsl:for-each-group select="*" group-starting-with="Items">
<topic>
  <xsl:attribute name="id">topic_<xsl:number count="Items | Item"/></xsl:attribute>
 <title><xsl:value-of select="Title"/></title>

  <xsl:for-each-group select="current-group() except ." group-starting-with="Item">
 <xsl:choose>
<xsl:when test="self::Item">
  <topic>
 <xsl:attribute name="id">topic_<xsl:number count="Items | Item"/></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="Body">
 <body>
 <xsl:apply-templates/>
 </body>
  </xsl:template>

 <xsl:template match="p">
 <p><xsl:apply-templates/></p>
  </xsl:template>

</xsl:stylesheet>

Output I got as:

<topic id="topic_1">
<title>Mam</title>
<topic id="topic_2">
<title>cyst1?</title>
<body>
<p>knee1.</p>
<p>knee2.</p>
</body>
</topic>
<topic id="topic_3">
<title>cyst2?</title>
<body>
<p>knee3.</p>
</body>
</topic>
</topic>  

Expected Output:

<topic id="topic_1">
<title>Mam</title>
<topic id="topic_2">
<title>cyst1?</title>
<body>
<section id="section1"><title>aaa</title>
<p>knee1.</p>
<section id="section2"><title>bbb</title>
<p>knee2.</p>
</section>
</section>      
</body>
</topic>
<topic id="topic_3">
<title>cyst2?</title>
<body>
<p>knee3.</p>
</body>
</topic>
</topic>

I need the h1 and h2 come as section with Id attribute and close as per the header order.

Please suggest me coding on this. Thanks in advance.

asked 14 secs ago

Let's block ads! (Why?)



I need to realign the element tag using XSLT

Aucun commentaire:

Enregistrer un commentaire