XSLT 2.0格式化

时间:2017-12-29 05:21:18

标签: xslt-2.0

截至目前,我正在获得1个区块的输出,但每当<condition rendition="online">它应该开始新的<option.lead&gt;标签。请帮助我获得所需的输出。

输入:

<option>
    <content>
        <top>
            <toptext align="center">ADDITIONAL</toptext>
        </top>
        <text.body>
            <body>
                <text ID="p16" level="1">
                test 123
                </text>
            </body>
            <body>
                <text ID="p17" level="1">
                 XYZ ABC 1234
                </text>
            </body>
        </text.body>
        <conditional partially="y" rendition="online">
            <top>
                <toptext align="center">FACTS</toptext>
            </top>
        </conditional>
        <conditional partially="y" rendition="online">
            <text.body>
                <body>
                    <text ID="p16" level="1">
                test 123
                    </text>
                </body>
                <body>
                    <text ID="p17" level="1">
                 XYZ ABC 1234
                    </text>
                </body>
                <body>
                    <text ID="p18" level="1">
                xyz abc 1234
                    </text>
                </body>
            </text.body>
            <top>
                <toptext align="center">ANALYSIS</toptext>
            </top>
            <top>
                <toptext align="center">
                    <csc>Fact Finding</csc>
                </toptext>
            </top>
        </content>
    </option>

样式表:

                                                                                                    

  <xsl:template name="addOptionBody">
    <xsl:element name="option.body">
        <xsl:apply-templates select="*" mode="OptionBody">
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

  <xsl:template match="top" mode="OptionBody">
       <xsl:element name="top">
             <xsl:apply-templates select="*" mode="OptionBody"/>
       </xsl:element>
  </xsl:template>

  <xsl:template match="toptext" mode="OptionBody">
    <xsl:element name="toptext">
        <xsl:call-template name="centerAlign"/>
            <xsl:apply-templates select="node()" mode="body"/>
    </xsl:element>
</xsl:template>

  <xsl:template match="content/text.body" mode="OptionBody">
    <xsl:element name="text.body">
        <xsl:apply-templates select="*" mode="OpinionBody">
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

 <xsl:template match="content/text.body/body" mode="OptionBody">
    <xsl:element name="body">
        <xsl:apply-templates select="*" mode="OpinionBody">
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

<xsl:template match="content/text.body/body/text" mode="OptionBody">
    <xsl:element name="body">
        <xsl:apply-templates select="node()" mode="body"/>
    </xsl:element>
</xsl:template>

实际结果:

<option.block>
        <option.block.body>
            <option.lead>
                <option.body>
                    <top>
                        <toptext>ADDITIONAL</toptext>
                    </top>
                    <text.body>
                        <body>
                            <text>
                    test 123
                            </text>
                        </body>
                        <body>
                            <text>
                     XYZ ABC 1234
                            </text>
                        </body>
                    </text.body>
                    <top>
                        <toptext>FACTS</toptext>
                    </top>
                    <text.body>
                        <body>
                            <text>
                    test 123
                            </text>
                        </body>
                        <body>
                            <text>
                     XYZ ABC 1234
                            </text>
                        </body>
                        <body>
                            <text>
                    xyz abc 1234
                            </text>
                        </body>
                    </text.body>
                    <top>
                        <toptext>ANALYSIS</toptext>
                    </top>
                    <top>
                        <toptext>
                            <csc>Fact Finding</csc>
                        </toptext>
                    </top>
                </option.body>
            </option.lead>
        </option.block.body>
    </option.block>

必填结果:

<option.block>
        <option.block.body>
            <option.lead>
                <option.body>
                    <top>
                        <toptext>ADDITIONAL</toptext>
                    </top>
                    <text.body>
                        <body>
                            <text>
                    test 123
                            </text>
                        </body>
                        <body>
                            <text>
                     XYZ ABC 1234
                            </text>
                        </body>
                    </text.body>
                </option.body>
            </option.lead>
            <option.lead>
                <option.body>
                    <top>
                        <toptext>FACTS</toptext>
                    </top>
                </option.body>
            </option.lead>
            <option.lead>
                <option.body>
                    <text.body>
                        <body>
                            <text>
                    test 123
                            </text>
                        </body>
                        <body>
                            <text>
                     XYZ ABC 1234
                            </text>
                        </body>
                        <body>
                            <text>
                    xyz abc 1234
                            </text>
                        </body>
                    </text.body>
                    <top>
                        <toptext>ANALYSIS</toptext>
                    </top>
                    <top>
                        <toptext>
                            <csc>Fact Finding</csc>
                        </toptext>
                    </top>
                </option.body>
            </option.lead>
        </option.block.body>
    </option.block>

1 个答案:

答案 0 :(得分:0)

听起来好像要使用for-each-grouping group-starting-with

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="option">
        <option.block>
            <xsl:apply-templates/>
        </option.block>
    </xsl:template>

    <xsl:template match="content">
        <option.block.body>
            <xsl:for-each-group select="*" group-starting-with="conditional[@rendition = 'online']">
                <xsl:choose>
                    <xsl:when test="not(self::conditional[@rendition = 'online'])">
                        <option.lead>
                            <option.body>
                                <xsl:apply-templates select="current-group()"/>
                            </option.body>
                        </option.lead>
                    </xsl:when>
                    <xsl:otherwise>
                        <option.lead>
                            <option.body>
                                <xsl:apply-templates select="current-group()/*"/>
                            </option.body>
                        </option.lead>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </option.block.body>
    </xsl:template>

    <xsl:template match="body">
        <text.body>
            <xsl:apply-templates/>
        </text.body>
    </xsl:template>

    <xsl:template match="text/@* | toptext/@*"/>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/eiQZDbc