使用 xslt 将 xml 转换为 jsonx [英] convert xml to jsonx using xslt
问题描述
任何人都可以帮助我使用数组获取以下内容..我必须生成通用的 xsl..输入 XML:
<登录><组><组><姓名>约翰</姓名><密码/></组><组><姓名>约翰</姓名><密码/></组></组></登录>
输出:
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><json:object name="登录"><json:object name="Groups"><json:array name="组"><json:object><json:string name="Name">john</json:string><json:string name="密码"/></json:object><json:object><json:string name="Name">john</json:string><json:string name="密码"/></json:object></json:array></json:object></json:object></json:object>
更通用的解决方案.需要 XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:template match="/"><xsl:element name="json:object"><xsl:apply-templates/></xsl:element></xsl:模板><xsl:template match="*[*]"><xsl:param name="nodeName" select="name()"/><xsl:variable name="firstNodeName" select="name(*[1])"/><xsl:element name="json:object"><xsl:if test="$nodeName"><xsl:attribute name="name" select="$nodeName"/></xsl:if><xsl:when test="(count(*) > 1) and (*/name() 中的每个 $x 都满足 $x=$firstNodeName)"><xsl:element name="json:array"><xsl:attribute name="name" select="$firstNodeName"/><xsl:apply-templates ><xsl:with-param name="nodeName" select="''"/></xsl:apply-templates></xsl:element></xsl:when><xsl:否则><xsl:apply-templates/></xsl:否则></xsl:选择></xsl:element></xsl:模板><xsl:template match="*[not(*)]"><xsl:element name="json:string"><xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute><xsl:value-of select="text()"/></xsl:element></xsl:模板></xsl:stylesheet>
应用于提供的示例 XML,产生以下输出:
以上解决方案已在此站点上进行了测试:http://xslttest.appspot.com/>
*/name() 中的每个 $x 都满足 $x=$firstNodeName
是 XPATH 2.0 构造,用于检查 */name()
序列中的所有元素是否为等于 $firstNodeName.所以整个条件实际上意味着检查一个节点是否有多个同名的子节点 - 这是检查我们是否正在处理 json:array
案例的条件.
Can anyone help me out in getting the below with an array..I have to generate the generalised xsl.. Input XML:
<Login>
<Groups>
<Group>
<Name>john</Name>
<Password/>
</Group>
<Group>
<Name>john</Name>
<Password/>
</Group>
</Groups>
</Login>
Output:
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<json:object name="Login">
<json:object name="Groups">
<json:array name="Group">
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"/>
</json:object>
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"/>
</json:object>
</json:array>
</json:object>
</json:object>
</json:object>
More generalized solution. Requires XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:element name="json:object">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="*[*]">
<xsl:param name="nodeName" select="name()" />
<xsl:variable name="firstNodeName" select="name(*[1])" />
<xsl:element name="json:object">
<xsl:if test="$nodeName">
<xsl:attribute name="name" select="$nodeName" />
</xsl:if>
<xsl:choose>
<xsl:when test="(count(*) > 1) and (every $x in */name() satisfies $x=$firstNodeName)">
<xsl:element name="json:array">
<xsl:attribute name="name" select="$firstNodeName" />
<xsl:apply-templates >
<xsl:with-param name="nodeName" select="''" />
</xsl:apply-templates>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:element name="json:string">
<xsl:attribute name="name">
<xsl:value-of select="name()" />
</xsl:attribute>
<xsl:value-of select="text()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Applied to the provided sample XML, produces following output:
<?xml version="1.0" encoding="UTF-8"?>
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<json:object name="Login">
<json:object name="Groups">
<json:array name="Group">
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"></json:string>
</json:object>
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"></json:string>
</json:object>
</json:array>
</json:object>
</json:object>
</json:object>
The above solution was tested on this site: http://xslttest.appspot.com/
EDIT:
every $x in */name() satisfies $x=$firstNodeName
is XPATH 2.0 construction that checks if all the elements in */name()
sequence are equal to $firstNodeName. So this whole condition actually means checking if a node has more than one child with the same name - this is a condition for checking if we're dealing with json:array
case.
这篇关于使用 xslt 将 xml 转换为 jsonx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!