XSLT 2.0格式化

问题描述 投票:0回答:1

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

输入:

<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>
xslt-2.0
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.