简体   繁体   中英

How to choose a attributes from XML using the XSLT?

    <students>
    <student student-name="Leo">
    <program> BIT </program> 
    <study-model> on-campus </study-model>
    </student>
    <student student-name="Dan">
    <program> MIT </program> 
    <study-model> off-campus </study-model>
    </student>
    </students>

can anyone help me ? im still a newbie for XML - XSLT .. i was trying to develop the XSTL for the XML data above

like i just want to choose the student name Dan , including his Program and study model for the output of the XML ..

what code should i use to choose the attributes from the XML ?

the example of the output will be like this:

just a bit difference with the data (using student name not student number , etc)

You can do something like the following:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="yes" />

    <xsl:template match="students">
        <table>
        <xsl:apply-templates select="student" />
        </table>
    </xsl:template>

    <xsl:template match="student">
        <tr>
                    <td>
                Student Name: <xsl:value-of select="@student-name" />
            </td>

            <xsl:apply-templates />
        </tr>
    </xsl:template>

    <xsl:template match="*">
        <td>
            <xsl:value-of select="name(.)" />: <xsl:value-of select="." />
        </td>
    </xsl:template>

</xsl:stylesheet>

This will output:

<table>
<tr>
<td>
                Student Name: Leo</td>
    <td>program:  BIT </td> 
    <td>study-model:  on-campus </td>

</tr>
<tr>
<td>
                Student Name: Dan</td>
    <td>program:  MIT </td> 
    <td>study-model:  off-campus </td>

</tr>
</table>

You can style this table by adding classes to the td in xsl, then adding css.

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