用于聊天日志的 XSLT:仅在该值的第一个实例上显示节点值?
我正在将 XSLT 样式表应用于 XML 聊天日志,并且希望仅在聊天者的名称第一次出现在连续的消息组中时显示该名称,以便每个消息组中的聊天者可以对多行进行“分组”。一个例子更好地说明了这一点。
我想从这个:
<Cthon98> hey, if you type in your pw, it will show as stars
<Cthon98> ********* see!
<AzureDiamond> hunter2
<AzureDiamond> doesnt look like stars to me
到这个:
<Cthon98> hey, if you type in your pw, it will show as stars
********* see!
<AzureDiamond> hunter2
doesnt look like stars to me
我的 XSL(每个聊天行迭代一次)是这样的:
<xsl:template match="User">
<!-- add a comma before all but the first user -->
<xsl:if test="position() != 1">, </xsl:if>
<!-- Pseudocode:
1. Set variable to name of current chatter
2. Set variable to name of previous line's chatter
3. If current chatter == previous chatter, don't display name
4. If current chatter != previous chatter, display name
-->
<!-- This displays the name -->
<xsl:value-of select="@FriendlyName"/>
</xsl:template>
有人可以帮我转换该伪代码吗?非常感谢!
编辑:输入 XML 本质上是以下往返消息结构的重复:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="20">
<Message>
<From><User FriendlyName="chatter1"/></From>
<To><User FriendlyName="chatter2"/></To>
<Text>hey</Text>
</Message>
<Message>
<From><User FriendlyName="chatter2"/></From>
<To><User FriendlyName="chatter1"/></To>
<Text>hey!</Text>
</Message>
</Log>
I'm applying an XSLT stylesheet to an XML chat log and want to only display the chatter's name the first time it appears in consecutive groups of messages, so that multiple lines are "grouped" by chatter in each message group. An example illustrates this better.
I want to go from this:
<Cthon98> hey, if you type in your pw, it will show as stars
<Cthon98> ********* see!
<AzureDiamond> hunter2
<AzureDiamond> doesnt look like stars to me
To this:
<Cthon98> hey, if you type in your pw, it will show as stars
********* see!
<AzureDiamond> hunter2
doesnt look like stars to me
My XSL (which is iterated once per chat line) is this:
<xsl:template match="User">
<!-- add a comma before all but the first user -->
<xsl:if test="position() != 1">, </xsl:if>
<!-- Pseudocode:
1. Set variable to name of current chatter
2. Set variable to name of previous line's chatter
3. If current chatter == previous chatter, don't display name
4. If current chatter != previous chatter, display name
-->
<!-- This displays the name -->
<xsl:value-of select="@FriendlyName"/>
</xsl:template>
Can someone help me convert that pseudocode? Thanks so much!
Edit: the input XML is essentially a repetition of the following to/from message structure:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="20">
<Message>
<From><User FriendlyName="chatter1"/></From>
<To><User FriendlyName="chatter2"/></To>
<Text>hey</Text>
</Message>
<Message>
<From><User FriendlyName="chatter2"/></From>
<To><User FriendlyName="chatter1"/></To>
<Text>hey!</Text>
</Message>
</Log>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
防止同一用户的连续消息显示名称的最简单方法是检查
preceding-sibling
轴。以下样式表:应用于以下输入:
产生以下输出:
编辑: 我可能会在自己的模板中处理每种情况,如下所示:
The easiest way to prevent the name from showing for consecutive messages from the same user is to check the
preceding-sibling
axis. The following stylesheet:Applied to the following input:
Produces the following output:
Edit: I would probably handle each case in its own template, like this: