需要帮助编写常见的 xsl [英] Need help in writing common xsl
本文介绍了需要帮助编写常见的 xsl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须生成 XML 输出.它应该显示数组,如后所示.我无法以正确的方式呈现数组.
I have to generate XMl output. It should display arrays as shown later. I am unable to render the arrays in the proper way.
输入 XML:
<accounts>
<displayOrdinal>0</displayOrdinal>
<name>String</name>
<account>
<accountNumber>String</accountNumber>
<name>String</name>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<usage>
<type>String</type>
</usage>
<usage>
<type>String</type>
</usage>
</account>
<account>
<accountNumber>String</accountNumber>
<name>String</name>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<balance>
<balanceAmount>0.0</balanceAmount>
</balance>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<properties>
<displayOrdinal>0</displayOrdinal>
</properties>
<usage>
<type>String</type>
</usage>
<usage>
<type>String</type>
</usage>
</account>
</accounts>
我的预期输出应该如下:
My expected output should be as follows:
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<json:object name="accounts">
<json:string name="displayOrdinal">0</json:string>
<json:string name="name">String</json:string>
<json:array name="account">
<json:object>
<json:string name="accountNumber">String</json:string>
<json:string name="name">String</json:string>
<json:array name="balance">
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
</json:array>
<json:array name="properties">
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
</json:array>
<json:array name="usage">
<json:object>
<json:string name="type">String</json:string>
</json:object>
<json:object name="usage">
<json:string name="type">String</json:string>
</json:object>
</json:array>
</json:object>
<json:object>
<json:string name="accountNumber">String</json:string>
<json:string name="name">String</json:string>
<json:object name="balance">
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:array name="balance">
<json:object>
<json:string name="balanceAmount">0.0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
<json:object>
<json:string name="displayOrdinal">0</json:string>
</json:object>
</json:array>
<json:array name="usage">
<json:object>
<json:string name="type">String</json:string>
</json:object>
<json:object>
<json:string name="type">String</json:string>
</json:object>
</json:array>
</json:object>
</json:array>
</json:object>
</json:object>
我使用的 XSL 如下:
The XSL that I am using is as below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- Array -->
<xsl:template match="*[*[2]][name(*[1])=name(*[2])]">
<json:object name="{name()}">
<json:array name="{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="*">
<xsl:choose>
<xsl:when test="text()">
<json:string name="{name()}">
<xsl:value-of select="."/>
</json:string>
</xsl:when>
<xsl:otherwise>
<json:object name="{name()}">
<xsl:apply-templates/>
</json:object>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- String -->
<xsl:template match="*[not(*)]">
<xsl:choose>
<xsl:when test="not(boolean(text()))">
<xsl:element name="json:null">
<xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="text()= 'false' or text()='true'">
<xsl:element name="json:boolean">
<xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<json:string name="{name()}">
<xsl:if test="@*">
<xsl:attribute name="{name(@*)}"><xsl:value-of select="@*"/></xsl:attribute>
</xsl:if>
<xsl:value-of select="."/>
</json:string>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
推荐答案
祝贺 Tim 第一个正确的解决方案.
Congrats to Tim for first correct solution.
我的解决方案并没有更好,但我将其作为一个有趣的问题呈现出来.它产生与 Tim 相同的输出,而且我已经合并了 OP 的原始解决方案尝试建议的 null 和 boolean 类型.
My solution is not any better, but I present it as a matter of interest. It produces the same output as Tim's, plus I have incorporated the null and boolean types suggested by the OP's original solution attempt.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
exclude-result-prefixes="xsl xs">
<xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/" name="nameless-object">
<json:object>
<xsl:apply-templates />
</json:object>
</xsl:template>
<!-- Array -->
<xsl:template match="*
[ following-sibling::*[1][name()=name(preceding-sibling::*[1])] ]
[not( preceding-sibling::*[1][name()=name(following-sibling::*[1])])]">
<xsl:variable name="array-name" select="name()" />
<xsl:variable name="followers" select="
(.|following-sibling::*)[name()=$array-name]" />
<xsl:variable name="stop" select="
(following-sibling::*[name()!=$array-name][1] |
../*[last()])[1]" />
<xsl:variable name="preceders" select="
($stop| $stop/preceding-sibling::*)[name()=$array-name]" />
<xsl:variable name="members" select="
$preceders[count(.|$followers)=count($followers)]" />
<json:array name="{$array-name}">
<xsl:for-each select="$members">
<xsl:call-template name="nameless-object" />
</xsl:for-each>
</json:array>
</xsl:template>
<xsl:template match="*
[preceding-sibling::*[1][name()=name(following-sibling::*[1])] ]" />
<!-- Object -->
<xsl:template match="*">
<json:object name="{name()}">
<xsl:apply-templates />
</json:object>
</xsl:template>
<!-- String -->
<xsl:template match="*[not(*)]">
<json:string name="{name()}">
<xsl:value-of select="." />
</json:string>
</xsl:template>
<!-- Null -->
<xsl:template match="*[not(*)][.='']">
<json:null name="{name()}" />
</xsl:template>
<!-- Boolean -->
<xsl:template match="*[not(*)][.='true' or .='false']">
<json:boolean name="{name()}">
<xsl:value-of select="." />
</json:boolean>
</xsl:template>
</xsl:stylesheet>
这篇关于需要帮助编写常见的 xsl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文