2017-05-07 59 views
0

xsl和.css有一个xml文档。我得到结果,但不是每个custID获得一行,每行都有一行,但每行都包含所有的客户。不知道我做错了什么。 --- XSLxsl每行重复而不是每个客户一行

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" 
     doctype-system="about:legacy-compat" 
     encoding="UTF-8" 
     indent="yes" /> 

    <xsl:param name="customerID" select="//@custID"/> 
    <xsl:variable name="reportCustomer" 
    select="document('customers.xml')/customers/customer[@custID=$customerID]" /> 

    <xsl:template match="/"> 
     <html> 
     <head> 
      <title>Customer List</title> 
      <link href="customersAgain.css" rel="stylesheet" type="text/css" /> 
     </head> 

     <body> 
      <div id="wrap"> 

       <h1>Customers</h1> 

<!-- <xsl:for-each select = "$customerID"> --> 

       <table id="customerTable"> 
        <tr> 
        <th>CustID</th> 
        <th>Cust FName</th> 
        <th>Cust LName</th>      
        <th>Address</th> 
        <th>Phone</th> 
        <th>Email</th> 
        </tr> 
<xsl:for-each select = "$customerID">   
        <tr> 
        <td> 
         <xsl:value-of select="$customerID" /> 
        </td>    
        <td> 
         <xsl:value-of select="$reportCustomer/first_name" /> 
        </td> 
        <td> 
         <xsl:value-of select="$reportCustomer/last_name" /> 
        </td> 
        <td> 
         <xsl:value-of select="$reportCustomer/street" /><br /> 
         <xsl:value-of select="$reportCustomer/city" />, 
         <xsl:value-of select="$reportCustomer/state" /> 
         <xsl:value-of select="$reportCustomer/zip" /> 
        </td> 
        <td> 
         <xsl:value-of select="$reportCustomer/phone" /> 
        </td> 
        <td> 
         <xsl:value-of select="$reportCustomer/email" /> 
        </td> 
        </tr> 
        <xsl:apply-templates select="customers/customer[@custID=$customerID]" /> 
</xsl:for-each>    
       </table> 

      <!--  <xsl:apply-templates select="customers/customer[@custID=$customerID]" /> 
</xsl:for-each> --> 
      </div> 
     </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

---- XML

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="customersAgain.xsl" ?> 
<customers xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
      xs:noNamespaceSchemaLocation="customers.xsd"> 
    <customer custID = "cust10001"> 
     <first_name>Mina</first_name> 
     <last_name>Talver</last_name> 
     <street>1121 Huntington Road</street> 
     <city>Morehead</city> 
     <state>SD</state> 
     <zip>52541</zip> 
     <phone>(252) 123-8444</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10002"> 
     <first_name>Joe</first_name> 
     <last_name>Crater</last_name> 
     <street>526 Corona Lane</street> 
     <city>Raleigh</city> 
     <state>SC</state> 
     <zip>52615</zip> 
     <phone>(506) 123-5124</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10003"> 
     <first_name>Linda</first_name> 
     <last_name>bigby</last_name> 
     <street>74 Center Court</street> 
     <city>Spaeder</city> 
     <state>AL</state> 
     <zip>85947</zip> 
     <phone>(885) 123-5156</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10004"> 
     <first_name>Bradley</first_name> 
     <last_name>Goetzman</last_name> 
     <street>552 Walker Street</street> 
     <city>Biloxy</city> 
     <state>MA</state> 
     <zip>52361</zip> 
     <phone>(852) 123-2252</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10005"> 
     <first_name>Donna</first_name> 
     <last_name>Lipsky</last_name> 
     <street>954 Mannard Way</street> 
     <city>Carhart</city> 
     <state>FL</state> 
     <zip>52398</zip> 
     <phone>(954) 123-6251</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10006"> 
     <first_name>Heath</first_name> 
     <last_name>Braden</last_name> 
     <street>58 East State Street</street> 
     <city>Silo Rapids</city> 
     <state>MN</state> 
     <zip>52632</zip> 
     <phone>(232) 123-8545</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10007"> 
     <first_name>Charisma</first_name> 
     <last_name>Wingard</last_name> 
     <street>45 Beverly Way</street> 
     <city>Paris</city> 
     <state>TX</state> 
     <zip>75858</zip> 
     <phone>(856) 123-7458</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10008"> 
     <first_name>Drake</first_name> 
     <last_name>Sorento</last_name> 
     <street>332 Kia Street</street> 
     <city>Carrito</city> 
     <state>NM</state> 
     <zip>55636</zip> 
     <phone>(864) 123-4565</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10009"> 
     <first_name>Jessica</first_name> 
     <last_name>Denon</last_name> 
     <street>562 Arthur Circle</street> 
     <city>Oak Tree</city> 
     <state>WY</state> 
     <zip>88787</zip> 
     <phone>(123) 123-5262</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10010"> 
     <first_name>Becky</first_name> 
     <last_name>Newton</last_name> 
     <street>45 North Avenue</street> 
     <city>Capitulo</city> 
     <state>TX</state> 
     <zip>55698</zip> 
     <phone>(415) 123-5445</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10011"> 
     <first_name>Rodger</first_name> 
     <last_name>Comancho</last_name> 
     <street>852 Leaf Boulevard</street> 
     <city>Warren</city> 
     <state>OK</state> 
     <zip>63589</zip> 
     <phone>(777) 123-2052</phone> 
     <email>[email protected]</email> 
    </customer> 
    <customer custID = "cust10012"> 
     <first_name>Mike</first_name> 
     <last_name>Doger</last_name> 
     <street>63 willingsway Drive</street> 
     <city>Saint Aug</city> 
     <state>CA</state> 
     <zip>36258</zip> 
     <phone>(121) 123-1221</phone> 
     <email>[email protected]</email> 
    </customer> 
</customers> 

寻找结果:

CustID  Cust FName Cust LName Address       Phone  Email 
cust10001 Mina  Talver  1121 Huntington Road, Morehead, SD 52541 (252) 123-8444 [email protected] 

cust10002 Joe Crater 526 Corona Lane, Raleigh, SC 52615 (506) 123-5124 [email protected] 

等 希望有人能帮助!谢谢

回答

1

你需要改变:

<xsl:value-of select="$reportCustomer/first_name" /> 

到:

<xsl:value-of select="../first_name" /> 

等。否则,您将从$reportCustomer文档的根目录开始,并选择所有first_name节点。改用当前客户数据的相对路径。


P.S.我不确定customer[@custID=$customerID]应该完成的条件是什么 - 在我看来,它永远是真的。或者您为什么需要使用document()函数,而不是直接使用XML输入。

此外,该行不有道理(幸运的是没有做别的其一),至少不是在@custID背景:

<xsl:apply-templates select="customers/customer[@custID=$customerID]" /> 

我认为你可以简化你的样式表是这样的:

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

<xsl:template match="/customers"> 
    <html> 
     <head> 
      <title>Customer List</title> 
      <link href="customersAgain.css" rel="stylesheet" type="text/css" /> 
     </head> 
     <body> 
      <div id="wrap"> 
       <h1>Customers</h1> 
       <table id="customerTable"> 
        <tr> 
         <th>CustID</th> 
         <th>Cust FName</th> 
         <th>Cust LName</th> 
         <th>Address</th> 
         <th>Phone</th> 
         <th>Email</th> 
        </tr> 
        <xsl:for-each select="customer"> 
         <tr> 
          <td> 
           <xsl:value-of select="@custID" /> 
          </td> 
          <td> 
           <xsl:value-of select="first_name" /> 
          </td> 
          <td> 
           <xsl:value-of select="last_name" /> 
          </td> 
          <td> 
           <xsl:value-of select="street" /> 
           <br /> 
           <xsl:value-of select="city" /> 
           <xsl:text>, </xsl:text> 
           <xsl:value-of select="state" /> 
           <xsl:text> </xsl:text> 
           <xsl:value-of select="zip" /> 
          </td> 
          <td> 
           <xsl:value-of select="phone" /> 
          </td> 
          <td> 
           <xsl:value-of select="email" /> 
          </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </div> 
     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 
相关问题