开发者

Colorize Unique Elements in XML using XSLT

开发者 https://www.devze.com 2023-01-03 12:06 出处:网络
I currently have an XML document which basically consists of several conversations between people just like an IM conversation.

I currently have an XML document which basically consists of several conversations between people just like an IM conversation.

I have each conversation displaying how I want to so far, except I want each name to be a unique color for readability.

How I have it is that the XML is transformed into HTML with CSS. I'd like to use XPath and XSL 1.0 for this:

XML

<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</dialogDa开发者_JS百科te>
      <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>

Ideally what I would want is to get an output of <p class="speaker one"> for the first speaker, <p class="speaker two"> for the 2nd and so on.

I was trying to use and the Meunchian Method to find how many unique speakers I have but what I have was not working:

...
<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> 
...

When I input 'Jaymes' or 'Stubbymush' I'd get the correct number of times that speaker spoke, but not how many speakers were in the conversation total.

Thanks in advance and if you have any suggestions for simpler methods because I'm overcomplicating it please advise.


This transformation:

<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>

when applied on the provided XML document, produces the correct speaker count:

2
0

精彩评论

暂无评论...
验证码 换一张
取 消