我目前有一个 XML 文档,它基本上由人与人之间的多个对话组成,就像 IM 对话一样。
我让每个对话都显示到目前为止我想要的方式,除了我希望每个名字都是一种独特的颜色以提高可读性。
我是如何使用 CSS 将 XML 转换为 HTML 的。我想为此使用 XPath 和 XSL 1.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”时,我会得到说话者说话的正确次数,但不会得到对话中说话者的总数。
在此先感谢您,如果您对更简单的方法有任何建议,因为我过于复杂了,请提出建议。
最佳答案
这个转换:
<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 文档时,生成正确的发言者人数:
2
关于html - 使用 XSLT 为 XML 中的唯一元素着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054284/