我目前有一个XML文档,它基本上由人与人之间的几个对话组成,就像IM对话一样。
我让每个对话都显示到目前为止我想要的方式,除了为了可读性而希望每个名称都是唯一的颜色。
我是如何将XML转换成带有CSS的HTML的。我想使用XPath和XSL1.0来实现这一点:
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</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方法来找出我有多少独特的扬声器,但我的方法不起作用:
...
<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”时,我会得到该说话者发言的正确次数,但不会得到对话中有多少说话者。
提前感谢,如果你对简单的方法有任何建议,因为我把它过于复杂了,请给我建议。
发布于 2010-06-17 00:06:05
此转换
<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文档上应用时,会生成正确的说话者计数。
2https://stackoverflow.com/questions/3054284
复制相似问题