当未定义数组长度时,XSLT 删除 Json 数组中的最后一个逗号

XSLT remove last comma in Json Array when Array Length is not defined

我必须根据多个业务逻辑构建 JSON 包含作者详细信息的负载。为此,我在 WSO2 EI 中使用 XSLT 2.0。下面提到了 xslt 的示例。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://example.com/x">
    <xsl:output method="text" encoding="utf-8" />
    <xsl:param name="param1" />
    <xsl:param name="param2" />
    <xsl:param name="param3" />
    <xsl:param name="param4" />
    <xsl:param name="param5" />

    <xsl:function name="x:setAuthor">
        <xsl:param name="p1" />
        <xsl:param name="p2" />
        <xsl:text>
            {"name": "</xsl:text><xsl:value-of select="$p1" /><xsl:text>",
             "age": "</xsl:text><xsl:value-of select="$p2" /><xsl:text>"
             },</xsl:text>
    </xsl:function>

    <xsl:template match="/">
        <xsl:text>
        {"booksDetails": [{
   "book": {"title": "</xsl:text><xsl:value-of select="$param1" /><xsl:text>"},
   "authors":    [
   </xsl:text>
        <xsl:choose>
            <xsl:when test="$param1 = 'book-sample1'">
                <xsl:value-of select="x:setAuthor('John', '35')" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="$param2 = 'true' and $param3 = 1">
                    <xsl:value-of select="x:setAuthor('Perera', '40')" />
                    <xsl:value-of select="x:setAuthor('Mark', '50')" />
                </xsl:if>
                <xsl:if test="$param2 = 'true' and $param4 = 'true' and $param5 = 'true'">
                    <xsl:value-of select="x:setAuthor('Larry', '60')" />
                    <xsl:value-of select="x:setAuthor('Elen', '28')" />
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
        <!-- There are lots of logics to set authors -->
        <xsl:text>
   ]
}]}
    </xsl:text>
    </xsl:template>
</xsl:stylesheet>

这里我有一个创建公共作者元素的函数。如下图。

        {
            "name": "<name>",
            "age": "<age>"
         },

其他一切正常,但问题是,这也为最后一个 author 元素设置了一个逗号 (,)。感谢您提出解决此问题的宝贵意见。

注意:在这种情况下,我无法使用以下内容,因为我不知道数组计数。可以根据逻辑变化。

<xsl:if test="position() != last()">
     <xsl:text>,</xsl:text>
</xsl:if>

提前致谢!

首先将作者存储在变量中,然后创建您的 json 数组。 即:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
  xmlns:x="http://example.com/x" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="utf-8" method="text"/>
  <xsl:param name="param1"/>
  <xsl:param name="param2"/>
  <xsl:param name="param3"/>
  <xsl:param name="param4"/>
  <xsl:param name="param5"/>

  <xsl:function name="x:setAuthor">
    <xsl:param name="name"/>
    <xsl:param name="age"/>
    <xsl:text> {"name": "</xsl:text><xsl:value-of select="$name"/><xsl:text>", "age": "</xsl:text> <xsl:value-of select="$age"/><xsl:text>"}</xsl:text>
  </xsl:function>

  <xsl:template match="/">
    <xsl:variable as="element(author)*" name="autheurs">
      <xsl:choose>
        <xsl:when test="$param1 = 'book-sample1'">
          <author age="35" name="John"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="$param2 = 'true' and $param3 = 1">
            <author age="40" name="Perera"/>
            <author age="50" name="Mark"/>
          </xsl:if>
          <xsl:if test="$param2 = 'true' and $param4 = 'true' and $param5 = 'true'">
            <author age="60" name="Larry"/>
            <author age="28" name="Elen"/>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:text>{"booksDetails": [{
         "book": {"title": "</xsl:text> <xsl:value-of select="$param1"/><xsl:text>"},
         "authors": [
    </xsl:text>
    <xsl:for-each select="$autheurs">
      <xsl:value-of select="x:setAuthor(@name, @age)"/>
      <xsl:if test="position() != last()">
        <xsl:text>,</xsl:text>
      </xsl:if>
    </xsl:for-each>
    <!-- There are lots of logics to set authors -->
    <xsl:text>
         ]
      }]}
          </xsl:text>
  </xsl:template>
</xsl:stylesheet>