【问题标题】:XSL display similar nodes same levelXSL 显示相同级别的相似节点
【发布时间】:2015-07-23 01:07:14
【问题描述】:

我有一个这样的xml数据。

    <commentList>
     <item>
        <comment_id>2</comment_id>
        <discussion_id>3</discussion_id>
        <replyTo>0</replyTo>
        <Text>This is a 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 to This is a test comment</Text>
     </item>
    </commentList>

replyTo - 父评论 ID(0 = 根)

replyTo 最多上一级。

我只想先显示评论和相关回复。然后下一个评论和回复等等。有没有最好的存档方法?提前致谢。

根据上述问题的预期输出。

This is a test comment
- Reply to This is a test comment
Hello

【问题讨论】:

  • 你能发布你的预期输出吗?
  • “显示”是指在浏览器中访问网页吗?
  • StackOverflow 不是一个代码编写服务,您可以在其中发布您的需求,然后有人免费为您实现它们。我们在这里为您提供现有代码的帮助,因此请发布它。
  • @michael.hor257k 这样的预期输出。 1.这是一条测试评论 2.回复这是一条测试评论 3.你好
  • @Perera1987 请编辑您的问题并添加预期的输出作为代码

标签: xml xslt


【解决方案1】:

使用key 将项目链接到他们的回复很方便。

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>

这对级别的数量没有限制。例如,给定以下测试输入:

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 to One</Text>
     </item>
    <item>
        <comment_id>4</comment_id>
        <replyTo>2</replyTo>
        <Text>Reply to Two</Text>
     </item>
     <item>
        <comment_id>5</comment_id>
        <replyTo>1</replyTo>
        <Text>Reply to One</Text>
     </item>
     <item>
        <comment_id>6</comment_id>
        <replyTo>3</replyTo>
        <Text>Reply to Three</Text>
     </item>
     <item>
        <comment_id>7</comment_id>
        <replyTo>3</replyTo>
        <Text>Reply to Three</Text>
     </item>
     <item>
        <comment_id>8</comment_id>
        <replyTo>4</replyTo>
        <Text>Reply to Four</Text>
     </item>
</commentList>

结果将是:

One
- Reply to One
- Reply to Three
- Reply to Three
- Reply to One
Two
- Reply to Two
- Reply to Four

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多