【问题标题】:Appending nodes with similar names using xslt in wso2在 wso2 中使用 xslt 附加具有相似名称的节点
【发布时间】:2023-01-30 21:57:43
【问题描述】:

我有一些带有多个名称节点的 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 如下所示。但这会为更新创建两个节点,为创建创建一个节点。而我希望 1st 和 3rd Name 节点位于更新下的节点和第二个名称节点创建

<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>


在转换为 json 时,我希望将相似的节点附加到一个数组中。像这样

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

我怎样才能在 XSL 1.0 中实现这一点?

【问题讨论】:

  • 您的样式表生成 XML,而不是 JSON。请将确切的预期结果作为 XML 发布。

标签: json xml wso2 xslt-1.0 wso2-integration-studio


【解决方案1】:

我猜你想要:

<xsl:template match="/Names">
    <Update>
        <xsl:copy-of select="CustName[id]"/>
    </Update>
    <Create>
        <xsl:copy-of select="CustName[not(id)]"/>
    </Create>
</xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多