【问题标题】:converting xml to jsonx format将xml转换为jsonx格式
【发布时间】:2019-08-20 22:47:56
【问题描述】:

我正在尝试将 XML 有效负载转换为 jsonx 格式。当输入 XML 具有属性时,我的代码不起作用,请您帮忙。应用程序元素应该以 json:object 的形式出现,但它以 json:string 的形式出现 我可以知道如何将 Applicaton 元素保留为 json:object 吗?

下面是我正在尝试的代码: 我用下面的代码测试

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/"  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" version="1.0"> 
    <xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="*[local-name()='Envelope']">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*[local-name()='Body']">
        <json:object name="Body">
            <xsl:apply-templates/>
        </json:object>
    </xsl:template>
    <!-- Array -->
    <xsl:template match="*[*[2]][name(*[1]) = name(*[2])]">
        <json:object name="{local-name()}">
            <json:array name="{local-name(*[1])}">
                <xsl:apply-templates/>
            </json:array>
        </json:object>
    </xsl:template>
    <!-- Array member -->
    <xsl:template match="*[parent::*[ name(*[1])=name(*[2]) ]] | /">
        <json:object>
            <xsl:apply-templates/>    
        </json:object>
    </xsl:template>
    <!-- Object -->
    <xsl:template match="*">
        <json:object name="{local-name()}">     
            <xsl:apply-templates select="@*|node()"/>
        </json:object>
    </xsl:template>
    <!-- String -->
    <xsl:template match="*[not(*)]">
        <json:string name="{local-name()}">
            <xsl:value-of select="."/>          
            <xsl:apply-templates select="@*"/>          
        </json:string>  
    </xsl:template>

    <xsl:template match="@*">
        <json:string name="{local-name()}">
            <xsl:value-of select="."/>                  
        </json:string>     
    </xsl:template> 

</xsl:stylesheet>

输入 XML: 输入我正在使用的有效负载 xml

 <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <SendEmailResponse xmlns="http://webservices.abcd.com/">
                <SendEmailResult>
                    <EmailSpecifications xmlns="">
                        <Status EmailID="0" Success="N">
                            <ErrorCode>1010</ErrorCode>
                            <ErrorDescription>Invalid AppID</ErrorDescription>
                        </Status>
                        <Application AppID="0" EntityID="0" />
                    </EmailSpecifications>
                </SendEmailResult>
            </SendEmailResponse>
        </soap:Body>
    </soap:Envelope>

输出我现在得到的 jsonx: 在输出中期待 &lt;json:object name="Application"&gt; 但得到 &lt;json:string name="Application"&gt;

<json:object
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
>

    <json:object name="Body">
        <json:object name="SendEmailResponse">
            <json:object name="SendEmailResult">
                <json:object name="EmailSpecifications">
                    <json:object name="Status">
                        <json:string name="EmailID">0</json:string>
                        <json:string name="Success">N</json:string>
                        <json:string name="ErrorCode">1010</json:string>
                        <json:string name="ErrorDescription">Invalid AppID</json:string>
                    </json:object>
                    <json:string name="Application">
                        <json:string name="AppID">0</json:string>
                        <json:string name="EntityID">0</json:string>
                    </json:string>
                </json:object>
            </json:object>
        </json:object>
    </json:object>

</json:object>

预期输出:需要将输出 XML 中的&lt;json:string name="Application"&gt; 更改为&lt;json:object name="Application"&gt;

    <json:object
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
>

    <json:object name="Body">
        <json:object name="SendEmailResponse">
            <json:object name="SendEmailResult">
                <json:object name="EmailSpecifications">
                    <json:object name="Status">
                        <json:string name="EmailID">0</json:string>
                        <json:string name="Success">N</json:string>
                        <json:string name="ErrorCode">1010</json:string>
                        <json:string name="ErrorDescription">Invalid AppID</json:string>
                    </json:object>
                    <json:object name="Application">
                        <json:string name="AppID">0</json:string>
                        <json:string name="EntityID">0</json:string>
                    </json:object>
                </json:object>
            </json:object>
        </json:object>
    </json:object>

</json:object>

【问题讨论】:

  • 你输入的xml正确吗?
  • 嗨金属,感谢您查看我的查询。我已经更新了我的 xsl 并且还有一点问题,请您再检查一下。我已经尝试了一些东西,并且接近了我正在寻找的东西

标签: xml xslt xslt-1.0 xslt-2.0 jsonx


【解决方案1】:
<xsl:template match="*[not(*) and not(@*)]">

或使用

<xsl:template match="*[not(*) and string-length(.) &gt; 0]">

代替你的字符串模板

<xsl:template match="*[not(*)]">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多