使用多个条目标记将JSONX示例转换为XML

时间:2014-09-05 01:04:34

标签: xml xslt

首先,我非常感谢,因为这个论坛真的对学习者有帮助。我需要你的帮助

我正在尝试将jsonx转换为常规xml,我需要做的一些事情是

1)输出xml应该在" feed"标签,如果有多个条目标签进入href链接(最后),它应该附加条目的数量,如下所示)

2)如果一个条目领域来了请求,那么我的xml应该在下面

3)如果授权名称更改为“身份验证”,则在href链接的末尾,只有最后一部分将更改为身份验证。

带有多个条目标签的Jsonx示例

<json:object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd">
    <json:array name="entry">
        <json:object>
            <json:object name="content">
                <json:object name="Authorization"> <!--whenever this feild name changes, then the output xml should append this in href as shown in xml" -->
                    <json:object name="authorizedPeriod">
                        <json:string name="duration">0</json:string>
                        <json:string name="start">0</json:string>
                    </json:object>
                    <json:object name="publishedPeriod">
                        <json:string name="duration">0</json:string>
                        <json:string name="start">0</json:string>
                    </json:object>
                    <json:string name="accessToken">688b026c-665f-4994-9139-6b21b13fbeee</json:string>
                    <json:string name="status">1</json:string>
                    <json:string name="expires_in">31536000</json:string>
                    <json:string name="refresh_token">9b853368-f7c3-4a35-a0f3-6c406611eaa6</json:string>
                    <json:string name="thirdParty">data_custodian_admin</json:string>
                </json:object>
            </json:object>
            <json:string name="published">2014-05-12T13:02:03Z</json:string>
            <json:string name="updated">2014-05-12T13:02:03Z</json:string>
        </json:object>
        <json:object>
            <json:object name="entry">
                <json:object name="content">
                    <json:object name="Authorization">
                        <json:object name="authorizedPeriod">
                            <json:string name="duration">0</json:string>
                            <json:string name="start">0</json:string>
                        </json:object>
                        <json:object name="publishedPeriod">
                            <json:string name="duration">0</json:string>
                            <json:string name="start">0</json:string>
                        </json:object>
                        <json:string name="accessToken">809caf03-612e-4e89-94b1-6f86d83b1ef</json:string>
                        <json:string name="status">1</json:string>
                        <json:string name="expires_in">31536000</json:string>
                        <json:string name="refresh_token">9b853368-f7c3-4a35-a0f3-6c406611eaa6</json:string>
                        <json:string name="thirdParty">upload_admin</json:string>
                    </json:object>
                </json:object>
                <json:string name="published">2014-05-12T13:02:03Z</json:string>
                <json:string name="updated">2014-05-12T13:02:03Z</json:string>
            </json:object>
        </json:object>
    </json:array>
</json:object>

当请求有多个条目标记时,需要将其转换为xml以下

  <feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.w3.org/2005/Atom">
        <id>urn:uuid:2215a7c7-29be-455a-bc5e-bbe1ecfcb089</id>
        <title>Green Button Usage Feed</title>
        <updated>2014-09-04T16:19:38-07:00</updated>
        <link href="https://services.org/resource/Authorization" rel="self"/>
        <entry> 
         <id>urn:uuid:D1A987DA-F5AC-468B-9C03-FA7839BCC8F1</id>
              <link href="https://services.org/resource/Authorization" rel="up"/>
              <link href="https://services.org/resource/Authorization/1" rel="self"/>  <!-- for every one entry tag, the number at the end of the url  should increase by 1 and for the second entry tag the number should change to 2 as below -->
              <content>
                <Authorization> <!-- if the "Authorization" element changes, then in the href link  only the last part changes for ex: if element comes as Authentication then the href link will change to "https://services.org/resource/Authentication" -->

                    <authorizedPeriod>
                        <duration>0</duration>
                        <start>0</start>
                    </authorizedPeriod>
                    <publishedPeriod>
                        <duration>0</duration>
                        <start>0</start>
                    </publishedPeriod>
                    <accessToken>688b026c-665f-4994-9139-6b21b13fbeee</accessToken>
                    <status>1</status>
                    <expires_in>31536000</expires_in>
                    <refresh_token>9b853368-f7c3-4a35-a0f3-6c406611eaa6</refresh_token>

                    <thirdParty>data_custodian_admin</thirdParty>
                </Authorization>
            </content>
            <published>2014-05-12T13:02:03Z</published>
            <updated>2014-05-12T13:02:03Z</updated>
        </entry>
        <entry>
            <id>urn:uuid:D1A987DA-F5AC-468B-9C03-FA7839BCC8F1</id>
              <link href="https://services.org/resource/Authorization" rel="up"/>
              <link href="https://services.org/resource/Authorization/2" rel="self"/>  <!-- since this is second entry tag, number is changed to 2, only number will chnage the rest of the url is allways constant-->
            <content>
                    <Authorization>
                        <authorizedPeriod>
                            <duration>0</duration>
                            <start>0</start>
                        </authorizedPeriod>
                        <publishedPeriod>
                            <duration>0</duration>
                            <start>0</start>
                        </publishedPeriod>
                        <accessToken>809caf03-612e-4e89-94b1-6f86d83b1ef8</accessToken>
                        <status>1</status>
                        <expires_in>31536000</expires_in>
                        <refresh_token>9b853368-f7c3-4a35-a0f3-6c406611eaa6</refresh_token>
                        <thirdParty>upload_admin</thirdParty>
                    </Authorization>
                </content>
                <published>2014-05-12T13:02:03Z</published>
                <updated>2014-05-12T13:02:03Z</updated>
            </entry>
    </feed>

当只有一个单一的条目标签进入

时,需要将xml转换为如下所示
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.w3.org/2005/Atom">
        <id>urn:uuid:2215a7c7-29be-455a-bc5e-bbe1ecfcb089</id>
        <title>Green Button Usage Feed</title>
        <updated>2014-09-04T16:19:38-07:00</updated>
        <link href="https://services.org/resource/Authorization" rel="self"/>
        <entry> 
         <id>urn:uuid:D1A987DA-F5AC-468B-9C03-FA7839BCC8F1</id>
              <link href="https://services.org/resource/Authorization" rel="up"/>
              <link href="https://services.org/resource/Authorization/1" rel="self"/>  <!-- for every one entry tag, the number at the end of the url  should increase by 1 and for the second entry tag the number should change to 2 as below -->
              <content>
                <Authorization> <!-- if the "Authorization" element changes, then in the href link  only the last part changes for ex: if element comes as Authentication then the href link will change to "https://services.org/resource/Authentication" -->

                    <authorizedPeriod>
                        <duration>0</duration>
                        <start>0</start>
                    </authorizedPeriod>
                    <publishedPeriod>
                        <duration>0</duration>
                        <start>0</start>
                    </publishedPeriod>
                    <accessToken>688b026c-665f-4994-9139-6b21b13fbeee</accessToken>
                    <status>1</status>
                    <expires_in>31536000</expires_in>
                    <refresh_token>9b853368-f7c3-4a35-a0f3-6c406611eaa6</refresh_token>

                    <thirdParty>data_custodian_admin</thirdParty>
                </Authorization>
            </content>
            <published>2014-05-12T13:02:03Z</published>
            <updated>2014-05-12T13:02:03Z</updated>
        </entry>
    </feed>

这是我的XLST(我不知道如何使用for循环并在最后将计数附加到href链接。)

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:dp="http://www.datapower.com/extensions" 
    xmlns:dpconfig="http://www.datapower.com/param/config" 
    xmlns:dpfunc="http://www.datapower.com/extensions/functions"
    exclude-result-prefixes="dp" 
    extension-element-prefixes="dp dpconfig dpfunc date">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">


        <!-- Generate variables-->
        <xsl:variable name="vCurrentTime" select="date:date-time()"/>
        <xsl:variable name="vRandomSalt" select="dp:generate-uuid()"/>
        <!-- create the AI node -->
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <id>urn:uuid:<xsl:value-of select="$vRandomSalt"/>
            </id>
            <title>Datausage</title>
            <updated>
                <xsl:value-of select="$vCurrentTime"/>
            </updated>
            <link href="https://services.org/resource/Authorization/Authorization" rel="self"/>
            <xsl:copy-of select="."/> 
        </feed>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

要开始而不是在主模板中使用<xsl:copy-of select="." />,您需要执行以下操作:

 <xsl:apply-templates />

这允许您使用模板匹配来操作所有json标记。对于第一个此类模板,您需要一个模板来匹配json:array个对象:

<xsl:template match="json:array" priority="2">
    <xsl:apply-templates select="json:object" />
</xsl:template>

在这种情况下,只需跳过它们,然后选择您将在下一个模板中转换为json:object元素的子entry,如下所示:

<xsl:template match="json:array/json:object" priority="2">
    <xsl:element name="{../@name}" namespace="http://www.w3.org/2005/Atom">

        <!-- More code here -->

        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

(注意它如何从父数组中获取名称entry,以避免对单词entry本身进行硬编码)

<!-- More code here -->位,您可以输出每个idlink元素 entry。特别是,第二个link将输出如下:

<link href="https://services.org/resource/Authorization/{position()}" rel="self"/>

请注意此处使用属性值模板输出数字。花括号表示要计算的表达式,而不是字面输出。

编辑:要使其依赖于后代对象,它可能看起来像这样

<link href="https://services.org/resource/{.//json:object[@name='content']/json:object/@name}" rel="up"/>

(尽管可能值得将表达式放在变量中)

最后,您将拥有一个更通用的模板来操纵其他json元素

<xsl:template match="json:*[@name]">
    <xsl:element name="{@name}" namespace="http://www.w3.org/2005/Atom">
        <xsl:apply-templates />
    </xsl:element>
 </xsl:template>

(请注意,前两个模板的优先级是确保它们在此通用模板之前使用)

试试这个XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times"
    xmlns:dp="http://www.datapower.com/extensions" 
    xmlns:dpconfig="http://www.datapower.com/param/config" 
    xmlns:dpfunc="http://www.datapower.com/extensions/functions"
    xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
    xmlns="http://www.w3.org/2005/Atom"
    exclude-result-prefixes="dp" 
    extension-element-prefixes="dp dpconfig dpfunc date">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <!-- Generate variables-->
    <xsl:variable name="vCurrentTime" select="date:date-time()"/>
    <xsl:variable name="vRandomSalt" select="dp:generate-uuid()"/>

    <xsl:template match="/">
        <!-- create the AI node -->
        <feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <id>urn:uuid:<xsl:value-of select="$vRandomSalt" /></id>
            <title>Datausage</title>
            <updated>
                <xsl:value-of select="$vCurrentTime"/>
            </updated>
            <link href="https://services.org/resource/Authorization" rel="self"/>
            <xsl:apply-templates />
        </feed>
    </xsl:template>

    <xsl:template match="json:array" priority="2">
        <xsl:apply-templates select="json:object" />
    </xsl:template>

    <xsl:template match="json:array/json:object" priority="2">
        <xsl:element name="{../@name}" namespace="http://www.w3.org/2005/Atom">
            <id>urn:uuid:<xsl:value-of select="$vRandomSalt" /></id>
            <xsl:variable name="type" select=".//json:object[@name='content']/json:object/@name" />
            <link href="https://services.org/resource/{$type}" rel="up"/>
            <link href="https://services.org/resource/{$type}/{position()}" rel="self"/>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="json:*[@name]">
        <xsl:element name="{@name}" namespace="http://www.w3.org/2005/Atom">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

请注意,json的名称空间也必须在XSLT中定义。