首页 > 解决方案 > JSON 到 XML SXLT

问题描述

目前,我尝试将来自 Rocket Chat (RC) webhook 的 json 响应映射到 OTRS XSLT。

  1. 这是我从 RC 收到的。

{
   "_id":"4jA2dgTGzwsHSsiLd",
    "messages":[
      { 
         "username":"guest-3",
         "msg":"youuu",
         "ts":"2018-12-13T15:13:56.906Z", 
      },
      {
         "username":"agent",
         "msg":"hahahaha",
         "ts":"2018-12-13T15:14:06.268Z"
      },
      {
         "username":"agent",
         "msg":"mane tn?",
         "ts":"2018-12-13T15:14:08.817Z"
      },
   ]
}

  1. 以下是我当前的 OTRS XSLT 映射。

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

<xsl:variable name="title">
<xsl:value-of select="//type" />
</xsl:variable>
    
<xsl:variable name="custname">
<xsl:value-of select="//visitor/name" />
</xsl:variable>
    
<xsl:variable name="custemail">
<xsl:value-of select="//visitor/email/address" />
</xsl:variable>
    
<xsl:variable name="tn">
<xsl:value-of select="//tags" />
</xsl:variable>

<xsl:variable name="createtime">
<xsl:value-of select="//createdAt" />
</xsl:variable>
    
<xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="RootElement">
       <xsl:copy>
           <TicketNumber><xsl:value-of select="$tn" /></TicketNumber>
           <Article>
               <Body> ? </Body>
               <ContentType>text/html; charset=ISO-8859-15</ContentType>
               <Subject><xsl:value-of select="$title" /></Subject>
               <From><xsl:value-of select="$custemail" /></From>
               <ArticleType>webrequest</ArticleType>
               <SenderType>customer</SenderType>
               <To>Misc</To>
               <HistoryType>AddNote</HistoryType>
               <HistoryComment>Note from RC</HistoryComment>
           </Article>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
  </xsl:template>
  <xsl:template match="content" />
</xsl:stylesheet>

我想从 JSON 中获取 messages->username 和 messages->msg 的所有值,并将其传递给 Body 标签。也许像:

消息:你好世界 作者:guest3

Msg: 你好 By: agent

或者可能是 html 表?

提前感谢。

标签: jsonxmlxslt

解决方案


想通了..这应该放在标签中

<xsl:for-each select="//messages">
<xsl:value-of select="msg" />
&lt;br/&gt;
<xsl:value-of select="username" />
&lt;br/&gt;
&lt;br/&gt;
<xsl:if test="./following-sibling::cd">,</xsl:if>
</xsl:for-each>


推荐阅读