json - JSON 到 XML SXLT
问题描述
目前,我尝试将来自 Rocket Chat (RC) webhook 的 json 响应映射到 OTRS XSLT。
- 这是我从 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"
},
]
}
- 以下是我当前的 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 表?
提前感谢。
解决方案
想通了..这应该放在标签中
<xsl:for-each select="//messages">
<xsl:value-of select="msg" />
<br/>
<xsl:value-of select="username" />
<br/>
<br/>
<xsl:if test="./following-sibling::cd">,</xsl:if>
</xsl:for-each>
推荐阅读
- java - Spring Boot 相当于 Dropwizard 命令?
- java - 将枚举转换为字符串时的奇怪行为
- sqlite - 按第一个非空列排序 sql 查询结果
- reactjs - image 和 h4 元素在 React Material UI 中没有并排出现
- php - 用 PHP 替换 xml 标签内的文本
- python - python3中的异常处理
- plot - Prometheus:如何创建一个时间图,每 0.5 秒绘制一次仪表值,同时保持刮擦间隔为 10 秒
- database - PostgresSQL 数据库详细信息
- python - UFuncTypeError 添加两个 datetime 日期时
- authentication - Auth0:更改密码更改`返回`按钮重定向值