2015-05-21 42 views
0

我有這個xml,當我通過一個xsl文件時,其餘部分顯示爲正常。但是,屬性只顯示第一個。我如何着手列出循環中的每個屬性?另外,這是設計我的xsl文件的好方法嗎?XML屬性在每個xsl中都不會更改

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="rental.xsl"?> 
<rentalProperties> 
<property available="yes" contact="0499584010"> 
<type>house</type> 
<address> 
<streetNo>111</streetNo> 
<street>say, Burwood Road</street> 
<suburb>say, Hawthorn</suburb> 
<state>VIC</state> 
<zipcode>3122</zipcode> 
</address> 
</property> 
<property available="no" contact="0485776610"> 
<type>apartment</type> 
<address> 
<streetNo>111</streetNo> 
<street>say, Burwood Road</street> 
<suburb>say, Hawthorn</suburb> 
<state>VIC</state> 
<zipcode>3122</zipcode> 
</address> 
</property> 
</rentalProperties> 

我的XSL

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
    <body> 
     <table border="1"> 
     <tr> 
     <th>Availability</th> 
     <th>Contact</th> 
     <th>Type</th> 
     <th>Street Number</th> 
     <th>Street</th> 
     <th>Suburb</th> 
     <th>State</th> 
     <th>Zipcode</th> 
     </tr> 
     <xsl:for-each select="/rentalProperties/property"> 
     <tr> 
     <td> 
      <xsl:value-of select="/rentalProperties/property/@available" /> 

     </td> 
     <td> 
      <xsl:value-of select="/rentalProperties/property/@contact" /> 

     </td> 
     <td> 
      <xsl:value-of select="type" /> 

     </td> 
     <td>  
      <xsl:value-of select="address/streetNo" /> 
     </td> 
     <td>  
      <xsl:value-of select="address/street" /> 
     </td> 
     <td>  
      <xsl:value-of select="address/suburb" /> 
     </td> 
     <td>  
      <xsl:value-of select="address/state" /> 
     </td> 
     <td>  
      <xsl:value-of select="address/zipcode" /> 
     </td> 
     </tr> 
     </xsl:for-each> 
     </table> 
    </body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

回答

1

相反的:

<xsl:value-of select="/rentalProperties/property/@available" /> 

你需要使用:

<xsl:value-of select="@available" /> 

,因爲你已經在property上下文。從根開始,您的版本獲取第一個rentalProperties/property節點的available屬性的值。


請注意,您可以通過使用一個模板所有表格單元格簡化您的樣式表:

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

<xsl:template match="/rentalProperties"> 
    <html> 
     <body> 
      <table border="1"> 
       <tr> 
        <th>Availability</th> 
        <th>Contact</th> 
        <th>Type</th> 
        <th>Street Number</th> 
        <th>Street</th> 
        <th>Suburb</th> 
        <th>State</th> 
        <th>Zipcode</th> 
       </tr> 
       <xsl:for-each select="property"> 
        <tr>  
         <xsl:apply-templates select="@* | *"/> 
        </tr> 
       </xsl:for-each> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="@available | @contact | type | address/*"> 
    <td>  
     <xsl:value-of select="." /> 
    </td> 
</xsl:template> 

</xsl:stylesheet> 
+0

謝謝!有效! – JianYA