首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT对XML中的唯一元素进行着色

使用XSLT对XML中的唯一元素进行着色
EN

Stack Overflow用户
提问于 2010-06-16 22:37:34
回答 1查看 171关注 0票数 2

我目前有一个XML文档,它基本上由人与人之间的几个对话组成,就像IM对话一样。

我让每个对话都显示到目前为止我想要的方式,除了为了可读性而希望每个名称都是唯一的颜色。

我是如何将XML转换成带有CSS的HTML的。我想使用XPath和XSL1.0来实现这一点:

XML

代码语言:javascript
复制
<wtfwhispers xmlns="http://wtfwhispers.kicks-ass.org" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://wtfwhispers.kicks-ass.org wtfwhispers.xsd">
  <conversation uuid="Diedrick">
    <datePosted>2010-05-30</datePosted>
    <description>What a great description</description>
    <dialog>
      <dialogDate>2009-12-22</dialogDate>
      <whisper>
        <whisperTime>03:55:00</whisperTime>
        <speaker>Stubbymush</speaker>
        <babble>i said something here</babble>
      </whisper>
      <whisper>
        <whisperTime>03:56:00</whisperTime>
        <speaker>Jaymes</speaker>
        <babble>what did you say?</babble>
      </whisper>
      <whisper>
        <whisperTime>03:56:00</whisperTime>
        <speaker>Stubbymush</speaker>
        <babble>i said something here!</babble>
      </whisper>
      ...
      <whisper>
        <whisperTime>03:57:00</whisperTime>
        <speaker>Stubbymush</speaker>
        <babble>gawd ur dumb</babble>
      </whisper>
    </dialog>
  </conversation>

</wtfwhispers>

理想情况下,我希望第一个扬声器的输出为<p class="speaker one">,第二个扬声器的输出为<p class="speaker two">,依此类推。

我试图使用Meunchian方法来找出我有多少独特的扬声器,但我的方法不起作用:

代码语言:javascript
复制
...
<xsl:key name="speakerList" match="wtf:whisper" use="wtf:speaker" />

    <xsl:template match="/">
        <html lang="en">
        <body>
        <p>
            <xsl:value-of select="count( key( 'speakerList', wtf:speaker ) )" />
        </p>
        </body>
        </html>
    </xsl:template> 
...

当我输入“Jaymes”或“Stubbymush”时,我会得到该说话者发言的正确次数,但不会得到对话中有多少说话者。

提前感谢,如果你对简单的方法有任何建议,因为我把它过于复杂了,请给我建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-17 00:06:05

此转换

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://wtfwhispers.kicks-ass.org"
 >
 <xsl:output method="text"/>

 <xsl:key name="kSpeakerByVal" match="w:speaker" use="."/>

 <xsl:template match="/">
  <xsl:value-of select=
   "count(
          /*/*/*/w:whisper/w:speaker
                       [generate-id()
                       =
                        generate-id(key('kSpeakerByVal',.)[1])
                        ]
          )
   "/>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用时,会生成正确的说话者计数

代码语言:javascript
复制
2
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3054284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档