标头值在每个循环中出现两次

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

由于 ,我两次获得第二个标头

输入提要具有多个元素,因为我需要根据

Plan
值设置两个标题

<Details>
    <Detail>
        <Code>001</Code>
        <Type>Alt</Type>
        <Plan>N</Plan>
    </Detail>
    <Detail>
        <Code>002</Code>
        <Type>Alt</Type>
        <Plan>Y</Plan>
    </Detail>
    <Detail>
        <Code>003</Code>
        <Type>Alt</Type>
        <Plan>Y</Plan>
    </Detail>
    <Detail>
        <Code>004</Code>
        <Type>Alt</Type>
        <Plan>N</Plan>
    </Detail>
    <Detail>
        <Code>005</Code>
        <Type>Alt</Type>
        <Plan>N</Plan>
    </Detail>
</Details>

我尝试过的Xsl代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

 <xsl:template match="Details">
    <table>
        <tbody>
            <xsl:for-each select="Detail">
                <xsl:sort select="Plan" order="ascending"/>
                <xsl:choose>
                    <xsl:when test="position() = 1 and current()/Plan='N'">
                        <row>
                            <entry>
                                <p><b>Plan A</b></p>
                            </entry>
                        </row>
                    </xsl:when>
                    <xsl:when test="position() != 1 and current()/Plan='Y'[1]">
                        <row>
                            <entry>
                                <p><b>Plan B</b></p>
                            </entry>
                        </row>
                    </xsl:when>
                    </xsl:choose>
                        <row>
                            <entry>
                                <p>
                                    <xsl:value-of select="current()/Code"/>
                                    
                                </p>
                            </entry>
                        </row>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>
</xsl:transform>

对于我在xsl中提到的计划B标头来选择第一行,但是每个代码值都会出现两次,预期输出应该是

<table>
   <tbody>
      <row>
         <entry>
            <p><b>Plan A</b></p>
         </entry>
      </row>
      <row>
         <entry>
            <p>001</p>
         </entry>
      </row>
      <row>
         <entry>
            <p>004</p>
         </entry>
      </row>
      <row>
         <entry>
            <p>005</p>
         </entry>
      </row>
      <row>
         <entry>
            <p><b>Plan B</b></p>
         </entry>
      </row>
      <row>
         <entry>
            <p>002</p>
         </entry>
      </row>
      <row>
         <entry>
            <p>003</p>
         </entry>
      </row>
   </tbody>
</table>
xml xslt xslt-1.0 xslt-2.0
1个回答
0
投票

我想我会采取不同的方法:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/Details">
    <table>
        <tbody>
            <xsl:for-each-group select="Detail" group-by="Plan">
                <xsl:sort select="Plan"/>
                <row>
                    <entry>
                        <p>
                            <b>
                                <xsl:value-of select="if(Plan='N') then 'Plan A' else 'Plan B'"/>
                            </b>
                        </p>
                    </entry>
                </row>
                <xsl:for-each select="current-group()">
                    <row>
                        <entry>
                            <p>
                                <xsl:value-of select="Code"/>
                            </p>
                        </entry>
                    </row>
                </xsl:for-each>
            </xsl:for-each-group>
        </tbody>
    </table>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.