简体   繁体   English

如何使用XSLT从XML中选择属性?

[英]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 我仍然是XML-XSLT的新手。。我正在尝试为上述XML数据开发XSTL。

like i just want to choose the student name Dan , including his Program and study model for the output of the XML .. 就像我只想选择学生姓名Dan一样,包括XML和XML输出的程序和学习模型。

what code should i use to choose the attributes from the XML ? 我应该使用什么代码从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. 您可以通过在xsl中将类添加到td中,然后添加css来设置此表的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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