简体   繁体   English

在xslt中使用xsl:when

[英]Using xsl:when in xslt

In my XSLT I am outputting customers who have SIM cards involved with country code 49, area code 31 and number 124567. In the When I am outputting any customer who has confirmed identity = 0 in a certain colour which were involved with these numbers. 在我的XSLT我输出谁与国家代码49,区号31号124567.参与SIM卡的客户When我输出谁已经确认身份= 0的某种颜色而被卷入这些号码的任何客户。

I need another when which outputs customers who have more than one SIM card in a different colour also involved with the numbers country code 49, area code 31, number 124567. The customers who have more than one SIM card have customerID repeated more than once inside the SIMs/SIM node. when需要输出具有多个不同颜色的SIM卡的客户,并且还涉及国家/地区代码49,区域代码31,编号124567 when ,我还需要另一个。 when拥有多个SIM卡的客户在内部多次重复有customerID, SIMs / SIM节点。 I am not sure how to output customers with more than one SIM card in a different colour. 我不确定如何使用多个不同颜色的SIM卡输出客户。 I think I need two when s. 我想我when s when需要两个。

XML CODE: XML代码:

<database>
  <customers>
    <customer>
      <customerID>1</customerID>
      <surname>Aardvark</surname>
      <firstname>John</firstname>
      <streetAddress>101 High Street</streetAddress>
      <townName>Dorptown</townName>
      <countyName>Wicklow</countyName>
      <contractOrPrepaid>contract</contractOrPrepaid>
      <confirmedIdentity>1</confirmedIdentity>
    </customer>
    <customer>
      <customerID>2</customerID>
      <surname>Brown</surname>
      <firstname>James</firstname>
      <streetAddress>102 High Street</streetAddress>
      <townName>Dorptown</townName>
      <countyName>Wicklow</countyName>
      <contractOrPrepaid>prepaid</contractOrPrepaid>
      <confirmedIdentity>0</confirmedIdentity>
    </customer>
    <customer>
      <customerID>3</customerID>
      <surname>Brown</surname>
      <firstname>Jack</firstname>
      <streetAddress>103 Main Street</streetAddress>
      <townName>Dorpville</townName>
      <countyName>Cork</countyName>
      <contractOrPrepaid>prepaid</contractOrPrepaid>
      <confirmedIdentity>1</confirmedIdentity>
    </customer>
    <customer>
      <customerID>4</customerID>
      <surname>Carroll</surname>
      <firstname>Jack</firstname>
      <streetAddress>103 Main Street</streetAddress>
      <townName>Dorpton</townName>
      <countyName>Kilkenny</countyName>
      <contractOrPrepaid>prepaid</contractOrPrepaid>
      <confirmedIdentity>0</confirmedIdentity>
    </customer>
    <customer>
      <customerID>5</customerID>
      <surname>Brown</surname>
      <firstname>Peter</firstname>
      <streetAddress>103 Main Street</streetAddress>
      <townName>Dorpborough</townName>
      <countyName>Kilkenny</countyName>
      <contractOrPrepaid>contract</contractOrPrepaid>
      <confirmedIdentity>1</confirmedIdentity>
    </customer>
    <customer>
      <customerID>6</customerID>
      <surname>Brown</surname>
      <firstname>Xavier</firstname>
      <streetAddress>103 Main Street</streetAddress>
      <townName>Dorpley</townName>
      <countyName>Kilkenny</countyName>
      <contractOrPrepaid>contract</contractOrPrepaid>
      <confirmedIdentity>1</confirmedIdentity>
    </customer>
    <customer>
      <customerID>7</customerID>
      <surname>Brown</surname>
      <firstname>Xavier</firstname>
      <streetAddress>103 Main Street</streetAddress>
      <townName>Dorpton Bridge</townName>
      <countyName>Wexford</countyName>
      <contractOrPrepaid>prepaid</contractOrPrepaid>
      <confirmedIdentity>0</confirmedIdentity>
    </customer>
    <customer>
      <customerID>8</customerID>
      <surname>Brown</surname>
      <firstname>Xavier</firstname>
      <streetAddress>103 High Street</streetAddress>
      <townName>New Dorpley</townName>
      <countyName>Carlow</countyName>
      <contractOrPrepaid>prepaid</contractOrPrepaid>
      <confirmedIdentity>0</confirmedIdentity>
    </customer>
  </customers>
  <sims>
    <sim>
      <simID>1</simID>
      <areaCode>081</areaCode>
      <number>1234567</number>
      <customerID>1</customerID>
      <yearPurchased>1995</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>2</simID>
      <areaCode>081</areaCode>
      <number>1234568</number>
      <customerID>2</customerID>
      <yearPurchased>1995</yearPurchased>
      <monthPurchased>11</monthPurchased>
      <dayPurchsed>11</dayPurchsed>
    </sim>
    <sim>
      <simID>3</simID>
      <areaCode>081</areaCode>
      <number>1234569</number>
      <customerID>3</customerID>
      <yearPurchased>1996</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>4</simID>
      <areaCode>081</areaCode>
      <number>1234570</number>
      <customerID>4</customerID>
      <yearPurchased>1997</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>5</simID>
      <areaCode>081</areaCode>
      <number>1234571</number>
      <customerID>5</customerID>
      <yearPurchased>1998</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>6</simID>
      <areaCode>081</areaCode>
      <number>1234572</number>
      <customerID>6</customerID>
      <yearPurchased>1999</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>7</simID>
      <areaCode>081</areaCode>
      <number>1234573</number>
      <customerID>7</customerID>
      <yearPurchased>2000</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>8</simID>
      <areaCode>081</areaCode>
      <number>1234574</number>
      <customerID>8</customerID>
      <yearPurchased>2001</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>9</simID>
      <areaCode>081</areaCode>
      <number>1234575</number>
      <customerID>1</customerID>
      <yearPurchased>2001</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>10</simID>
      <areaCode>081</areaCode>
      <number>1234576</number>
      <customerID>2</customerID>
      <yearPurchased>2002</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>11</simID>
      <areaCode>081</areaCode>
      <number>1234577</number>
      <customerID>3</customerID>
      <yearPurchased>2003</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>12</simID>
      <areaCode>081</areaCode>
      <number>1234578</number>
      <customerID>4</customerID>
      <yearPurchased>2004</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>13</simID>
      <areaCode>081</areaCode>
      <number>1234579</number>
      <customerID>4</customerID>
      <yearPurchased>2005</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>14</simID>
      <areaCode>081</areaCode>
      <number>1234580</number>
      <customerID>5</customerID>
      <yearPurchased>2006</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>15</simID>
      <areaCode>081</areaCode>
      <number>1234581</number>
      <customerID>5</customerID>
      <yearPurchased>2007</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
    <sim>
      <simID>16</simID>
      <areaCode>081</areaCode>
      <number>1234582</number>
      <customerID>5</customerID>
      <yearPurchased>2008</yearPurchased>
      <monthPurchased>10</monthPurchased>
      <dayPurchsed>12</dayPurchsed>
    </sim>
  </sims>
  <calls>
    <call>
      <callID>12345678901234567890</callID>
      <simID>1</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>12</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567891</callID>
      <simID>2</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>13</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567892</callID>
      <simID>3</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>14</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567893</callID>
      <simID>4</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>15</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567894</callID>
      <simID>5</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>16</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567895</callID>
      <simID>6</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>17</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567896</callID>
      <simID>7</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>18</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567897</callID>
      <simID>8</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>19</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567898</callID>
      <simID>9</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>20</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567899</callID>
      <simID>10</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>44</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>123</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>21</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567900</callID>
      <simID>11</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>353</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>021</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>22</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567901</callID>
      <simID>12</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>353</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>056</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>7654321</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>23</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567902</callID>
      <simID>2</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>49</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>31</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>124567</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>24</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567903</callID>
      <simID>14</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>49</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>31</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>124567</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>25</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567904</callID>
      <simID>6</simID>
      <madeOrReceived>made</madeOrReceived>
      <countryCodeOfOtherParty>49</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>31</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>124567</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>26</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
    <call>
      <callID>12345678901234567905</callID>
      <simID>7</simID>
      <madeOrReceived>received</madeOrReceived>
      <countryCodeOfOtherParty>49</countryCodeOfOtherParty>
      <areaCodeOfOtherParty>31</areaCodeOfOtherParty>
      <numberCodeOfOtherParty>124567</numberCodeOfOtherParty>
      <year>2009</year>
      <month>10</month>
      <day>27</day>
      <hour>12</hour>
      <minute>12</minute>
      <second>5</second>
    </call>
  </calls>
</database>

XSLT CODE: XSLT代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="customerid" match="database/sims/sim" use="customerID" />

  <xsl:template match="/">
    <html>
      <head>
        <title>Customers</title>
      </head>
      <body>
        <table rules="all">
          <caption>Customers</caption>
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Surname</th>
            </tr>
          </thead>

          <xsl:for-each select="database/calls/call">

            <xsl:if test="countryCodeOfOtherParty  = '49' and areaCodeOfOtherParty ='31' and numberCodeOfOtherParty ='124567'">

              <xsl:variable name="callsimID" select="./simID"/>

              <xsl:for-each select= "/database/sims/sim[simID = $callsimID]">

                <xsl:variable name="simCustomerID" select="./customerID"/>

                <xsl:for-each select= "/database/customers/customer[customerID = $simCustomerID]">

                  <tr>
                    <xsl:choose>
                      <xsl:when test="confirmedIdentity=0">
                        <td bgcolor="#FFA500">
                          <xsl:value-of select="firstname"/>
                        </td>
                        <td bgcolor="#FFA500">
                          <xsl:value-of select="surname"/>
                        </td>
                      </xsl:when>

                    </xsl:choose>
                  </tr>

                </xsl:for-each>

              </xsl:for-each>
            </xsl:if>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Here's how you can make this XSLT much tidier and accomplish what you are describing: 这是使XSLT整洁并完成您所描述的方法:

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

  <xsl:key name="simsById" match="sim" use="simID" />
  <xsl:key name="customersById" match="customer" use="customerID" />
  <xsl:key name="simsByCustomerId" match="database/sims/sim" 
           use="customerID" />

  <xsl:template match="/">
    <html>
      <head>
        <title>Customers</title>
        <style>
          tr.unconfirmed td { background-color: #FFA500; }
          tr.multisim    td { background-color: Green; }
        </style>
      </head>
      <body>
        <table rules="all">
          <caption>Customers</caption>
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Surname</th>
            </tr>
          </thead>

          <xsl:apply-templates
              select="database/calls/call[countryCodeOfOtherParty  = '49' and
                                          areaCodeOfOtherParty ='31' and 
                                          numberCodeOfOtherParty ='124567']" />
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="call">
     <xsl:apply-templates select="key('customersById', 
                                      key('simsById', simID)/customerID)" />
  </xsl:template>

  <xsl:template match="customer">
    <tr>
      <xsl:attribute name="class">
        <xsl:apply-templates select="." mode="determineClass" />
      </xsl:attribute>
      <td>
        <xsl:value-of select="firstname"/>
      </td>
      <td>
        <xsl:value-of select="surname"/>
      </td>
    </tr>
  </xsl:template>

  <!-- Below is the part that determines a row's class -->

  <!-- Catch-all for customers with no special formatting -->
  <xsl:template match="customer" mode="determineClass" />

  <xsl:template match="customer[confirmedIdentity = 0]" mode="determineClass">
    <xsl:text>unconfirmed</xsl:text>
  </xsl:template>

  <xsl:template match="customer[key('simsByCustomerId', customerID)[2]]"
                mode="determineClass">
    <xsl:text>multisim</xsl:text>
  </xsl:template>

</xsl:stylesheet>

When run on your sample input, the result is: 在样本输入上运行时,结果为:

<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Customers</title>
    <style>
          tr.unconfirmed td { background-color: #FFA500; }
          tr.multisim    td { background-color: Green; }
        </style>
  </head>
  <body>
    <table rules="all">
      <caption>Customers</caption>
      <thead>
        <tr>
          <th>Firstname</th>
          <th>Surname</th>
        </tr>
      </thead>
      <tr class="multisim">
        <td>James</td>
        <td>Brown</td>
      </tr>
      <tr class="multisim">
        <td>Peter</td>
        <td>Brown</td>
      </tr>
      <tr class="">
        <td>Xavier</td>
        <td>Brown</td>
      </tr>
      <tr class="unconfirmed">
        <td>Xavier</td>
        <td>Brown</td>
      </tr>
    </table>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM