带有多条目标记的JSONX到XML示例
首先,我非常感谢,因为这个论坛对学习者真的很有帮助。我再次需要你的帮助 我正在尝试将jsonx转换为常规xml,这里需要做的几件事是 1输出xml应位于提要标记中,如果有多个条目标记进入,则在末尾href link,它应附加条目计数,如下所示 2如果请求中有一个条目,那么我的xml如下所示 3如果授权名称更改为身份验证,则在href链接的末尾,只有最后一部分将更改为身份验证 示例Jsonx附带多个条目标记带有多条目标记的JSONX到XML示例,xml,xslt,Xml,Xslt,首先,我非常感谢,因为这个论坛对学习者真的很有帮助。我再次需要你的帮助 我正在尝试将jsonx转换为常规xml,这里需要做的几件事是 1输出xml应位于提要标记中,如果有多个条目标记进入,则在末尾href link,它应附加条目计数,如下所示 2如果请求中有一个条目,那么我的xml如下所示 3如果授权名称更改为身份验证,则在href链接的末尾,只有最后一部分将更改为身份验证 示例Jsonx附带多个条目标记 <json:object xmlns:xsi="http://www.w3.org/
<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>
首先,您需要执行以下操作,而不是在主模板中使用:
<xsl:apply-templates />
在本例中,只需跳过它们,选择子json:object,然后在下一个模板中将其转换为条目元素,如下所示:
<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>
请注意,此处使用属性值模板来输出数字。大括号表示要计算的表达式,而不是字面上的输出
编辑:要使其依赖于子体对象,它可以如下所示
<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中定义。非常感谢XSLT。我非常感谢您的帮助…一个快速问题。非常感谢XSLT。我非常感谢您的帮助…一个快速问题。假设如果请求带有Authenticationafter content tag标记,而不是if Authorization,那么我如何更改href的结尾部分,就像我做了一次编辑,表明可以完成一样,如果我理解正确的话!如果没有,它可能会引导您走向正确的方向,至少……如果您遇到XSLT不太有效的情况,最好修改您原来的问题来说明这一点。非常感谢。嘿,蒂姆,我不明白。。。很抱歉
<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 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>