json 在wso2中使用xslt追加名称相似的节点

pokxtpni  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(113)

我有一些带有多个Name节点的xml数据。根据是否存在id节点,我需要分离节点。在转换为JSON时,我希望所有相似的节点合并到一个JSON数组中。下面是我的XML数据

<Names>
        <CustName>
            <Name>Name1</Name>
            <id>3</id>
        </CustName>
        <CustName >
            <Name>Name2</Name>
        </CustName>
        <CustName>
            <Name>Name3</Name>
            <id>32</id>
        </CustName>
    </Names>

我尝试的XSLT如下所示。但这会创建两个节点用于更新,一个节点用于创建。而我希望第一个和第三个名称节点位于Update节点下,第二个名称节点位于Create

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
        <xsl:template match="/">
            <CustomerNames>
                <xsl:for-each select="//Names/CustName">
                    <xsl:choose>
                        <xsl:when test="id !=''">
                            <Update>
                                <CustName>
                                    <xsl:value-of select="Name"/>
                                </CustName>
                                <id>
                                    <xsl:value-of select="id"/>
                                </id>
                            </Update>
                        </xsl:when>
                        <xsl:otherwise>
                            <Create>
                                <CustName>
                                    <xsl:value-of select="Name"/>
                                </CustName>
                            </Create>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </CustomerNames>
        </xsl:template>
    </xsl:stylesheet>

转换后的xml应如下所示:

<CustomerNames>
        <Update>
            <CustName>Name1</CustName>
            <id>3</id>
        </Update>
        <Update>
            <CustName>Name3</CustName>
            <id>32</id>
        </Update>
        <Create>
            <Name>Name2</Name>
        </Create>
    </CustomerNames>

在转换为json时,我希望类似的节点被附加到一个数组中。

{
   "CustomerNames":{
      "Update":[
         {
            "CustName":"Name1",
            "id":"3"
         },
         {
            "CustName":"Name3",
            "id":"32"
         }
      ],
      "Create":[
         {
            "Name":"Name2"
         }
      ]
   }
}

如何在XSL 1.0中实现这一点?

nhaq1z21

nhaq1z211#

在自动将XML转换为JSON时,节点的放置顺序似乎很重要。因此,请将XSLT更新为如下所示的内容。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <CustomerNames>
            <xsl:for-each select="//Names/CustName[id]">
                <Update>
                    <CustName>
                        <xsl:value-of select="Name"/>
                    </CustName>
                    <id>
                        <xsl:value-of select="id"/>
                    </id>
                </Update>
            </xsl:for-each>
            <xsl:for-each select="//Names/CustName[not(id)]">
                <Create>
                    <CustName>
                        <xsl:value-of select="Name"/>
                    </CustName>
                </Create>
            </xsl:for-each>
        </CustomerNames>
    </xsl:template>
</xsl:stylesheet>
6psbrbz9

6psbrbz92#

或者,可以使用XSL排序对XML节点进行分组

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <CustomerNames xmlns="">
            <xsl:for-each select="//Names/CustName">
                <xsl:sort select="id"/>
                <xsl:choose>
                    <xsl:when test="id !=''">
                        <Update>
                            <CustName>
                                <xsl:value-of select="Name"/>
                            </CustName>
                            <id>
                                <xsl:value-of select="id"/>
                            </id>
                        </Update>
                    </xsl:when>
                    <xsl:otherwise>
                        <Create>
                            <CustName>
                                <xsl:value-of select="Name"/>
                            </CustName>
                        </Create>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </CustomerNames>
    </xsl:template>
</xsl:stylesheet>
az31mfrm

az31mfrm3#

另一种方法是在XSLT中介器之后使用Payloadfactry中介器来对节点进行分组。

<payloadFactory media-type="xml">
    <format>
        <CustomerNames>
        $1
        $2
        </CustomerNames>
    </format>
    <args>
        <arg expression="//Update"/>
        <arg expression="//Create"/>
    </args>
</payloadFactory>

相关问题