我想从以下示例中提取键值对:
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="007957F616780001" timeShift="1" visibility="ALL" eventId="1">
<userInfo personId="" userNick="John Doe" userType="CLIENT" protocolType="FLEX" timeZoneOffset="120"/>
<userData>
<item key="GMSServiceId">5954d184-f89d-4f44-8c0f-a772d458b353</item>
<item key="IdentifyCreateContact">3</item>
<item key="MediaType">chat</item><item key="TimeZone">120</item>
<item key="_data_id">139-e9826bf5-c5a4-40e5-a729-2cbdb4776a43</item>
<item key="firstName">John</item><item key="first_name">John</item>
<item key="lastName">Doe</item>
<item key="last_name">Doe</item>
<item key="location_lat">37.8197</item>
<item key="location_long">-122.4786</item>
<item key="userDisplayName">John Doe</item>
</userData>
</newParty>
<newParty userId="0079581874B50010" timeShift="0" visibility="ALL" eventId="1">
<userInfo personId="" userNick="John" userType="CLIENT" protocolType="FLEX" timeZoneOffset="0"/>
<userData>
<item key="EmailAddress">dskim@gmail.com</item>
<item key="FirstName">John</item>
<item key="IdentifyCreateContact">3</item>
<item key="LastName">Doe</item>
<item key="MediaType">chat</item>
<item key="TimeZone">120</item>
</userData>
</newParty>
</chatTranscript>
例如,对于添加的第一个新用户,我想获取以下信息:
GMSServiceId:c0aa6221-d5f9-4fdc-9f75-ed63e99b1f12 IdentifyCreateContact:3,MediaType:chat,TimeZone:120,_ data_id:139-9a8ee95b-d3ba-43a2-93a9-08ed7965d63d,firstName:Mike,lastName:Kumm
我想为添加的其他新方提取类似的信息字符串 - 它应该取出关键字段中的文本值并附加文本值;所以直到所有的附加。
我不想事先指定字段列表 - 程序应该报告它获得的任何字段。
这是我现有的xslt,我开始在CLIENTs的部分匹配下实现一些解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:key name="party" match="newParty" use="@userId" />
<xsl:template match="@timeShift">
<xsl:attribute name="timeShift">
<xsl:call-template name="format-duration"/>
</xsl:attribute>
</xsl:template>
<xsl:template name="format-duration">
<xsl:param name="value" select="." />
<xsl:param name="alwaysIncludeHours" select="false()" />
<xsl:param name="includeSeconds" select="true()" />
<xsl:if test="$value > 3600 or $alwaysIncludeHours">
<xsl:value-of select="concat(format-number($value div 3600, '00'), ':')"/>
</xsl:if>
<xsl:value-of select="format-number($value div 60 mod 60, '00')" />
<xsl:if test="$includeSeconds">
<xsl:value-of select="concat(':', format-number($value mod 60, '00'))" />
</xsl:if>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/chatTranscript">
<html>
<head>
<link rel="stylesheet" type="text/css" href="/webrecall/css/chat.css"/>
</head>
<header><xsl:value-of select="translate(@startAt, 'TZ', ' ')"/></header>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="newParty[userInfo/@userType='CLIENT']">
<div class="ClientJoined" id="{@userId}">
<label>Client: <xsl:value-of select="userInfo/@userNick" />
</label>
<label class="moreUserInfo">
<xsl:value-of select="concat('
', name(), ' | ')"/>
</label>
<label class="timeShiftLabel">
<xsl:call-template name="format-duration">
<xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template match="newParty[userInfo/@userType='AGENT']">
<div class="AgentJoined" id="{@userId}">
<label>Agent: <xsl:value-of select="userInfo/@userNick" /></label>
<label class="timeShiftLabel">
<xsl:call-template name="format-duration"><xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template match="newParty[userInfo/@userType='EXTERNAL']">
<div class="SystemJoined" id="{@userId}">
<label>System: <xsl:value-of select="userInfo/@userNick" /></label>
<label class="timeShiftLabel System">
<xsl:call-template name="format-duration">
<xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template match="message">
<xsl:variable name="party-class">
<xsl:call-template name="lookup-class"/>
</xsl:variable>
<div class="Messages {$party-class}" id="{@eventId}">
<label><xsl:value-of select="msgText" /></label>
<label class="timeShiftLabel">
<xsl:call-template name="format-duration">
<xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template match="notice">
<xsl:variable name="party-class">
<xsl:call-template name="lookup-class"/>
</xsl:variable>
<div class="Notices {$party-class}" id="{@eventId}">
<label>
<xsl:value-of select="noticeText" />
</label>
<label class="timeShiftLabel">
<xsl:call-template name="format-duration">
<xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template match="partyLeft">
<xsl:variable name="party-class">
<xsl:call-template name="lookup-class"/>
</xsl:variable>
<div class="Notices {$party-class}" id="{@eventId}">
<label><xsl:value-of select="reason" /></label>
<label class="timeShiftLabel">
<xsl:call-template name="format-duration">
<xsl:with-param name="value" select="@timeShift"/>
</xsl:call-template>
</label>
</div>
</xsl:template>
<xsl:template name="lookup-class">
<xsl:variable name="party-type" select="key('party', @userId)/userInfo/@userType" />
<xsl:choose>
<xsl:when test="$party-type='CLIENT'">Client</xsl:when>
<xsl:when test="$party-type='AGENT'">Agent</xsl:when>
<xsl:when test="$party-type='EXTERNAL'">System</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
所需输出的格式不太清楚。我猜你想做点什么:
<xsl:template match="newParty[userInfo/@userType='CLIENT']">
<div class="ClientJoined" id="{@userId}">
<label>Client: <xsl:value-of select="userInfo/@userNick" />
</label>
<xsl:apply-templates select="userData/item"/>
</div>
</xsl:template>
<xsl:template match="item">
<label>
<xsl:value-of select="concat(@key, ': ', .)"/>
</label>
</xsl:template>
适用于您的示例输入的将返回:
<div class="ClientJoined" id="0079581AF5590023">
<label>Client: 10.50.24.202</label>
<label>GMSServiceId: c0aa6221-d5f9-4fdc-9f75-ed63e99b1f12</label>
<label>IdentifyCreateContact: 3</label>
<label>MediaType: chat</label>
<label>TimeZone: 120</label>
<label>_data_id: 139-9a8ee95b-d3ba-43a2-93a9-08ed7965d63d</label>
<label>firstName: Mike</label>
<label>lastName: Kumm</label>
</div>
<div class="ClientJoined" id="007957F616780001">
<label>Client: John Doe</label>
<label>GMSServiceId: 5954d184-f89d-4f44-8c0f-a772d458b353</label>
<label>IdentifyCreateContact: 3</label>
<label>MediaType: chat</label>
<label>TimeZone: 120</label>
<label>_data_id: 139-e9826bf5-c5a4-40e5-a729-2cbdb4776a43</label>
<label>firstName: John</label>
<label>first_name: John</label>
<label>lastName: Doe</label>
<label>last_name: Doe</label>
<label>location_lat: 37.8197</label>
<label>location_long: -122.4786</label>
<label>userDisplayName: John Doe</label>
</div>
<div class="ClientJoined" id="0079581874B50010">
<label>Client: John</label>
<label>EmailAddress: dskim@gmail.com</label>
<label>FirstName: John</label>
<label>IdentifyCreateContact: 3</label>
<label>LastName: Doe</label>
<label>MediaType: chat</label>
<label>TimeZone: 120</label>
</div>
注意:如果这应该是HTML label
元素,那么我认为这是错误的使用方法。