简体   繁体   中英

XML attribute not changing in xsl for each

I have this xml and when i put it through an xsl file, the rest appears as per normal. However, the attributes only show the first one. How do I go about listing each attribute in the loop? In addition, is this a good way to design my xsl file?

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

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

Instead of :

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

you need to use:

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

because you are already in the context of property . Your version gets the value of the available attribute of the first rentalProperties/property node, starting from the root.


Note that you could simplify your stylesheet by using a single template for all table cells:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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