xml - 从 xslt 中删除多余的字符
问题描述
我有以下示例 xml 输入。
<inputM xmlns="http://example.com">
<Title>test</Title>
<Gender>ffff</Gender>
<MiddleName>dere</MiddleName>
<Surname>qqq</Surname>
<PreferredName>ppp</PreferredName>
</inputM>
根据逻辑,我想生成一条 JSON 消息。为此,我在 xslt 下面使用了。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns="http://example.com" xmlns:func="http://exslt.org/functions">
<xsl:output method="text" omit-xml-declaration="yes" indent="no" encoding="UTF-8" media-type="application/json"/>
<xsl:template match="ns:inputM">
<xsl:text>{"outputX": {</xsl:text>
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:Title,'xxx','title')" />
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:Gender,'yyy','gender')" />
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:MiddleName,'zzz','middleName')" />
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:Surname,'qqq','surname')" />
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:PreferredName,'ppp','preferredName')" />
<xsl:text>}}</xsl:text>
</xsl:template>
<xsl:function name="ns:getChangedValue">
<xsl:param name="inputValue"/>
<xsl:param name="default"/>
<xsl:param name="jsonField"/>
<xsl:if test="$inputValue != $default">
<xsl:choose>
<xsl:when test="$inputValue = 'TEST'" >
<func:result>
<xsl:value-of select="concat('"',$jsonField,'": " removed')" />
<xsl:text>"</xsl:text>
<xsl:text>,</xsl:text>
</func:result>
</xsl:when>
<xsl:otherwise>
<func:result>
<xsl:value-of select="concat('"',$jsonField,'": "', $inputValue)" />
<xsl:text>"</xsl:text>
<xsl:text>,</xsl:text>
</func:result>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:function>
</xsl:stylesheet>
生成的输出如下所示。
{"outputX": {"title": "test","gender": "ffff","middleName": "dere",}}
在输出消息的最后一个元素之后总是有额外的“,”。有没有办法从单个 xslt 文件中避免这种情况
解决方案
您在函数中输出逗号,但显然该函数不知道是否会出现以下值。
解决它的一种方法是更改函数,使其不添加逗号,并使用xsl:value-of
带有separator
.
试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:ns="http://example.com" xmlns:func="http://exslt.org/functions">
<xsl:output method="text" omit-xml-declaration="yes" indent="no" encoding="UTF-8" media-type="application/json"/>
<xsl:template match="ns:inputM">
<xsl:text>{"outputX": {</xsl:text>
<xsl:value-of select="ns:getChangedValue(//ns:inputM/ns:Title,'xxx','title'),
ns:getChangedValue(//ns:inputM/ns:Gender,'yyy','gender'),
ns:getChangedValue(//ns:inputM/ns:MiddleName,'zzz','middleName'),
ns:getChangedValue(//ns:inputM/ns:Surname,'qqq','surname'),
ns:getChangedValue(//ns:inputM/ns:PreferredName,'ppp','preferredName')" separator=", " />
<xsl:text>}}</xsl:text>
</xsl:template>
<xsl:function name="ns:getChangedValue">
<xsl:param name="inputValue"/>
<xsl:param name="default"/>
<xsl:param name="jsonField"/>
<xsl:if test="$inputValue != $default">
<xsl:choose>
<xsl:when test="$inputValue = 'TEST'" >
<func:result>
<xsl:value-of select="concat('"',$jsonField,'": " removed')" />
<xsl:text>"</xsl:text>
</func:result>
</xsl:when>
<xsl:otherwise>
<func:result>
<xsl:value-of select="concat('"',$jsonField,'": "', $inputValue)" />
<xsl:text>"</xsl:text>
</func:result>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:function>
</xsl:stylesheet>
请注意,您确实应该将 XSLT 的版本号更改为“2.0”。
推荐阅读
- ansible - 将文件内容放在 Ansible 的电子邮件正文中
- r - 如何构建摘要数据框
- pdf - PDF 图像软掩模伪影
- bash - 根据输入的 csv 文件从 bash 文件调用具有多个实例的 C 二进制文件
- javascript - JavaScript 上的 ESLint 错误 (Unexpected token ...) 简化为字典
- javascript - 如何在 Angular 2 中包含 jquery 插件
- python - 如何在 Python 中找到两个字符串格式的日期之间的差异?
- python - 使用cupy时变量值无故改变
- css - 谷歌应用脚本包含来自另一个文件的 css
- javascript - 使用 jexcel 插件推送数组以自定义字幕编辑器