【问题标题】:XSLT on field with multiples nested objects具有多个嵌套对象的字段上的 XSLT
【发布时间】:2017-09-26 23:05:44
【问题描述】:

我刚刚为我当前的项目学习 XSLT,我遇到了一个问题,有人可以帮助我实现这种转换吗?

输入:

<response>
<num>5521187659301</num>
<object>1,20170109,20170504,14000.00|3,20170112,20170409,50000.00|4,19800229,20170422,2000000.00|</object>
</response>

输出:

<response>
    <num>5521187659301</num>
    <object>
        <object-child>
            <param1>1</param1>
            <param2>20170109</param2>
            <param3>20170504</param3>
            <param4>14000.00</param4>
        </object-child>
        <object-child>
            <param1>3</param1>
            <param2>20170112</param2>
            <param3>20170409</param3>
            <param4>50000.00</param4>
        </object-child>
        <object-child>
            <param1>4</param1>
            <param2>19800229</param2>
            <param3>20170422</param3>
            <param4>2000000.00</param4>
        </object-child>
    </object>
</response>

目前我处于这个阶段

<xsl:template match="/">
    <response>
    <num><xsl:value-of select="response/num"/></num>
    <object>
        <xsl:for-each select="response/object">
            <xsl:for-each select="tokenize(.,'|')">
                <object-child>                      

                </object-child>
            </xsl:for-each>
        </xsl:for-each>
     </object>
   </response>
 </xsl:template>

【问题讨论】:

    标签: xml api xslt soa


    【解决方案1】:

    显然,您需要标记两次:一次在外部 | 分隔符上(注意它需要转义),然后在内部逗号上。

    试试这个方法:

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="object">
         <xsl:copy>
            <xsl:for-each select="tokenize(., '\|')">
                <object-child>  
                    <xsl:for-each select="tokenize(., ',')">
                        <xsl:element name="param{position()}">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:for-each>
                </object-child>
            </xsl:for-each>
        </xsl:copy>   
    </xsl:template>
    
    </xsl:stylesheet>
    

    演示:http://xsltransform.net/gVhD8QE

    【讨论】:

    • @Vitor 如果您的问题得到解答,请通过接受答案来关闭它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2019-03-27
    • 1970-01-01
    • 2018-10-12
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多