2014-04-29 44 views
0

在我的XSLT我输出谁与国家代码49,区号31号124567.参与When我在一定的输出谁已经确认身份的客户= 0 SIM卡的客户与这些数字有关的颜色。使用XSL:当XSLT

我需要另一个when输出具有不同颜色的多个SIM卡的客户,这些SIM卡也涉及号码国家代码49,区号31,号码124567.具有多个SIM卡的客户重复了客户ID在SIM/SIM节点内不止一次。我不确定如何向客户输出具有不同颜色的多个SIM卡。我想我需要两个when s。

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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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>123456789</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代码:

<?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> 
+0

欢迎来到Stackoverflow。也许你可以减少输入XML到重现问题的最小输入。它还帮助,如果您有一句话用问号结束。 –

回答

1

这里是你如何让这个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> 

当运行您的样品输入,结果是:

<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>