【问题标题】:JSON to XML SXLTJSON 到 XML SXLT
【发布时间】:2018-12-14 03:55:54
【问题描述】:

目前,我尝试将来自 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

消息:你好 作者:代理

或者可能是 html 表格?

提前致谢。

【问题讨论】:

    标签: json xml xslt


    【解决方案1】:

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

    <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>

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      相关资源
      最近更新 更多