xml - XSL display similar nodes same level -


i have xml data this.

    <commentlist>      <item>         <comment_id>2</comment_id>         <discussion_id>3</discussion_id>         <replyto>0</replyto>         <text>this test comment</text>      </item>      <item>         <comment_id>3</comment_id>         <discussion_id>3</discussion_id>         <replyto>0</replyto>         <text>hello</text>      </item>      <item>         <comment_id>4</comment_id>         <discussion_id>3</discussion_id>         <replyto>2</replyto>         <text>reply test comment</text>      </item>     </commentlist> 

replyto - parent comment id (0 = root)

replyto goes maximum 1 level.

i want display comment , relevant replies first. next comment , replies , on. there best way archive this? in advance.

expected output per above question.

this test comment - reply test comment hello 

it's convenient use key link items replies.

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="utf-8"/> <xsl:strip-space elements="*"/>  <xsl:key name="replies" match="item" use="replyto" />  <xsl:template match="/commentlist">     <!-- select starter items -->     <xsl:apply-templates select="item[replyto=0]"/>  </xsl:template>  <xsl:template match="item">     <xsl:if test="replyto > 0">         <xsl:text>- </xsl:text>     </xsl:if>     <xsl:value-of select="text"/>     <xsl:text>&#10;</xsl:text>     <!-- append replies -->     <xsl:apply-templates select="key('replies', comment_id)"/>   </xsl:template>  </xsl:stylesheet> 

this places no limit number of levels. example, given following test input:

xml

<commentlist>       <item>         <comment_id>1</comment_id>         <replyto>0</replyto>         <text>one</text>      </item>      <item>         <comment_id>2</comment_id>         <replyto>0</replyto>         <text>two</text>      </item>      <item>         <comment_id>3</comment_id>         <replyto>1</replyto>         <text>reply one</text>      </item>     <item>         <comment_id>4</comment_id>         <replyto>2</replyto>         <text>reply two</text>      </item>      <item>         <comment_id>5</comment_id>         <replyto>1</replyto>         <text>reply one</text>      </item>      <item>         <comment_id>6</comment_id>         <replyto>3</replyto>         <text>reply three</text>      </item>      <item>         <comment_id>7</comment_id>         <replyto>3</replyto>         <text>reply three</text>      </item>      <item>         <comment_id>8</comment_id>         <replyto>4</replyto>         <text>reply four</text>      </item> </commentlist> 

the result be:

one - reply 1 - reply 3 - reply 3 - reply 1 2 - reply 2 - reply 4 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -