XML属性在每个xsl中都没有变化

时间:2015-05-21 16:21:17

标签: xml xslt

我有这个xml,当我把它放到xsl文件中时,其余部分按照正常情况显示。但是,属性仅显示第一个属性。如何在循环中列出每个属性?另外,这是设计我的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 个答案:

答案 0 :(得分:1)

而不是:

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

你需要使用:

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

因为您已经在property的上下文中。您的版本从根开始获取第一个 available节点的rentalProperties/property属性的值。

请注意,您可以通过为所有表格单元格使用单个模板来简化样式表:

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