Conditional Statements in XSLT when having to choose a tag from several elements

风格不统一 提交于 2019-12-04 19:59:41

You're falling into a trap here: you're seeing constructs like for-each and choose that look familiar from other languages, and you're instinctively using them in preference to other less familiar constructs. As a result, you're not writing your code "the XSLT way". The XSLT way is to use template rules: templates that match particular patterns occurring in the input. This is sometimes called "push processing" as distinct from "pull processing".

So this kind of code:

<xsl:choose>
      <xsl:when test="BookSegServ/BookSeg/DetSeg/BSServType='TUTU'">
      <td><xsl:value-of select="BookSegServ/BookSeg/DetSeg/BSServDest" /></td>
      <td><xsl:value-of select="BookSegServ/BookSeg/BSSservSpec/BSServQuota" /></td>

should be written like this:

<xsl:apply-templates select="BookSegServ/BookSeg"/>

<xsl:template match="BookSeg[DetSeg/BSServType='TUTU']">
  <td><xsl:value-of select="DetSeg/BSServDest"/></td>
  <td><xsl:value-of select="BSSservSpec/BSServQuota" /></td>
</xsl:template>

with further template rules that match other values of BSServType.

(I'm only trying to point you in the right direction here, not to provide working code).

Arigela

I managed to get the answer and get my XSLT working. I had the help of an expert but I thought I would share the answer: Michael Kay had put me on the right track but I had been unable to do it on my own but anyway here it is:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/AllBookingFnp">
        <html>
            <body>
                <h2>Liste des commandes</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Destination</th>
                        <th>BSServQuota</th>
                    </tr>
                    <xsl:apply-templates select="BookingFnp"></xsl:apply-templates>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="BookingFnp">
                <xsl:choose>
                    <xsl:when test="BookSegServ/BookSeg/DetSeg/BSServType='TUTU'">
                        <xsl:apply-templates select="BookSegServ/BookSeg/DetSeg[BSServType='TUTU']"/>
                    </xsl:when>
                    <xsl:when test="BookSegServ/BookSeg/DetSeg/BSServType='SOHO'">
                        <xsl:apply-templates select="BookSegServ/BookSeg/DetSeg[BSServType='SOHO']"/>
                    </xsl:when>
                    <xsl:when test="BookSegServ/BookSeg/DetSeg/BSServType='SOVI'">
                        <xsl:apply-templates select="BookSegServ/BookSeg/DetSeg[BSServType='SOVI']"/>
                    </xsl:when>
                </xsl:choose>
    </xsl:template>

    <xsl:template match="DetSeg">
        <tr>
            <td><xsl:value-of select="BSServDest" /></td>
            <td><xsl:value-of select="parent::BookSeg/BSSservSpec/BSServQuota" /></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

Thanks to everyone who tried to help me.

Here you have an example solution totally template driven (no loops, no choose):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="/AllBookingFnp">
        <html>
            <body>
                <h2>Liste des commandes</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Destination</th>
                        <th>BSServQuota</th>
                    </tr>
                    <xsl:apply-templates select="BookingFnp"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="BookingFnp">
        <tr>
            <xsl:apply-templates select="BookSegServ/BookSeg/DetSeg"/>
        </tr>
    </xsl:template>

    <xsl:template match="DetSeg[BSServType='TUTU']">
        <xsl:apply-templates select="BSServDest"/>
    </xsl:template>

    <xsl:template match="DetSeg[BSServType='SOHO' and 
        not(parent::BookSeg/
                    preceding-sibling::BookSeg[1]/
                        DetSeg[BSServType='TUTU'])]">
        <xsl:apply-templates select="BSServDest"/>
    </xsl:template>

    <xsl:template match="DetSeg[BSServType='SOVI' and 
        not(parent::BookSeg/
                    preceding-sibling::BookSeg/
                        DetSeg[BSServType='SOHO'] or
                parent::BookSeg/
                    preceding-sibling::BookSeg/
                        DetSeg[BSServType='TUTU']
        )]">
        <xsl:apply-templates select="BSServDest"/>
    </xsl:template>

    <xsl:template match="BSServDest">
        <td><xsl:value-of select="." /></td>
        <td><xsl:value-of select="
                parent::DetSeg/
                following-sibling::BSSservSpec/
                BSServQuota" /></td>
    </xsl:template>

    <xsl:template match="*"/>

</xsl:stylesheet>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!