首页 > 解决方案 > 从 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('&quot;',$jsonField,'&quot;: &quot; removed')" />
                        <xsl:text>"</xsl:text>
                        <xsl:text>,</xsl:text>
                        </func:result>
                    </xsl:when>
                    <xsl:otherwise>
                        <func:result>
                        <xsl:value-of select="concat('&quot;',$jsonField,'&quot;: &quot;', $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 文件中避免这种情况

标签: xmlxslt

解决方案


您在函数中输出逗号,但显然该函数不知道是否会出现以下值。

解决它的一种方法是更改​​函数,使其不添加逗号,并使用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('&quot;',$jsonField,'&quot;: &quot; removed')" />
                    <xsl:text>"</xsl:text>
                    </func:result>
                </xsl:when>
                <xsl:otherwise>
                    <func:result>
                    <xsl:value-of select="concat('&quot;',$jsonField,'&quot;: &quot;', $inputValue)" />
                    <xsl:text>"</xsl:text>
                    </func:result>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:function>
</xsl:stylesheet>

请注意,您确实应该将 XSLT 的版本号更改为“2.0”。


推荐阅读