XSLT到HTML,存储有关双方的信息,然后查找每个方的活动

时间:2017-02-07 12:30:06

标签: xml xslt

我有一个XML文件,我试图通过使用XSLT将其转换为动态HTML页面。

XML如下:

<?xml version="1.0"?>
<chatTranscript startAt="2016-11-03T08:29:13Z" sessionId="0001KaC1NSN60019">
    <newParty userId="0079581AF5590023" timeShift="0" visibility="ALL" eventId="1">
        <userInfo personId="" userNick="10.50.24.202" userType="CLIENT" protocolType="FLEX" timeZoneOffset="120"/>
        <userData>
            <item key="GMSServiceId">c0aa6221-d5f9-4fdc-9f75-ed63e99b1f12</item>
            <item key="IdentifyCreateContact">3</item><item key="MediaType">chat</item>
            <item key="TimeZone">120</item><item key="_data_id">139-9a8ee95b-d3ba-43a2-93a9-08ed7965d63d</item>
            <item key="firstName">Mike</item>
            <item key="lastName">Kumm</item>
        </userData>
    </newParty>
    
    <newParty userId="0079581AF56C0025" timeShift="20" visibility="ALL" eventId="2">
        <userInfo personId="1" userNick="allendei" userType="AGENT" protocolType="BASIC" timeZoneOffset="120"/>
    </newParty>
    
    <message userId="0079581AF56C0025" timeShift="32" visibility="ALL" eventId="4">
    <msgText treatAs="NORMAL">Hello</msgText></message>
    <message userId="0079581AF5590023" timeShift="62" visibility="ALL" eventId="5">
    <msgText msgType="text" treatAs="NORMAL">Can you help me?</msgText></message>
    <notice userId="007957F616780001" timeShift="246" visibility="ALL" eventId="3">
		    <noticeText noticeType="USER_CUSTOM">This is notice one.</noticeText>
    </notice>

    <notice userId="007957F616780001" timeShift="246" visibility="ALL" eventId="3">
	    <noticeText noticeType="USER_CUSTOM">This is notice two.</noticeText>
    </notice>



    <partyLeft userId="007957F616780001" timeShift="291" visibility="ALL" eventId="4" askerId="007957F616780001">
	    <reason code="3">left due to disconnect</reason>
    </partyLeft>
</chatTranscript>

我希望HTML看起来像的大致如下:

<html>
    <header>Session ID: 0001KaC1NSN60019</header>
    <div class="Client" id="0079581AF5590023">
        <label>Client: 10.50.24.202</label>
    </div>
    <div class="Agent" id="0079581AF56C0025">
        <label>Agent: allendei</label>
    </div>
    
    <div class="Messages" id="4">
        <label>Hello</label>
    </div>
    
    <div class="Messages" id="5">
        <label>Can you help me?</label>
    </div>
</html>

这是我想要的东西,必要的引用和关系(代理与客户端,他们的详细信息被记住,以及打印出来的消息时,实际将每条消息放入div中,并且正确类名,以确保我可以为每个方法应用不同的样式。

1 个答案:

答案 0 :(得分:1)

如果您的输出确实是所需的输出,请使用此XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:template match="/chatTranscript">
      <html>
        <header><xsl:value-of select="@sessionId" /></header>
        <xsl:apply-templates select="newParty" />
        <xsl:apply-templates select="message/msgText" />
      </html>
    </xsl:template>

    <xsl:template match="newParty[userInfo/@userType='CLIENT']">
      <div class="Client" id="{@userId}">
        <label>Client: <xsl:value-of select="userInfo/@userNick" /></label>
      </div>
    </xsl:template>

    <xsl:template match="newParty[userInfo/@userType='AGENT']">
      <div class="Client" id="{@userId}">
        <label>Agent: <xsl:value-of select="userInfo/@userNick" /></label>
      </div>
    </xsl:template>

    <xsl:template match="msgText">
      <div class="Messages" id="{../@eventId}">
        <label><xsl:value-of select="text()" /></label>
      </div>
    </xsl:template>

</xsl:stylesheet>

<强>输出:

<?xml version="1.0"?>
<html>
    <header>0001KaC1NSN60019</header>
    <div class="Client" id="0079581AF5590023">
        <label>Client: 10.50.24.202</label>
    </div>
    <div class="Client" id="0079581AF56C0025">
        <label>Agent: allendei</label>
    </div>
    <div class="Messages" id="4">
        <label>Hello</label>
    </div>
    <div class="Messages" id="5">
        <label>Can you help me?</label>
    </div>
</html>