从 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 文件中避免这种情况

C队

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

解决此问题的一种方法是更改​​函数,使其不添加逗号,并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”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章