【问题标题】:create a table sorted by attribute value创建一个按属性值排序的表
【发布时间】:2020-11-03 19:26:16
【问题描述】:

我是 xslt 的新手,我一直在努力寻找解决方案,我需要创建一个表格,其中我的 xml 文档按属性“级别”(此处为 niveau)排序,然后按字母顺序排列

这是 xml 文档(如果您想知道的话,它是法语)


   <dico
       xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
       xsi:noNamespaceSchemaLocation='dico.xsd'>
       <mot niveau="2">arbre</mot>
       <mot niveau="2">jeu</mot>
       <mot niveau="3">essence</mot>
       <mot niveau="5">convalescence</mot>
       <mot niveau="5">mot-valise</mot>
       <mot niveau="1">pain</mot>
       <mot niveau="4">maintenance</mot>
   
   </dico>

这是我的 xslt

<xsl:template match="/">
        <html>
            <head>
                <title>Dictionnaire</title>
            </head>
            <body>

                <xsl:apply-templates select="dico/mot">
                    <xsl:sort select="@niveau"/>
                    <xsl:sort select="."/>
                </xsl:apply-templates>



            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="mot">
        <table border="4" cellspacing="4" cellpadding="2" width="80%">
            <tr>
                <colgroup>
                    <col style="width: 33%" />
                    <col style="width: 33%" />
                    <col style="width: 33%" />
                </colgroup>
                <td>
                    le mot est : <xsl:value-of select="."/>
                </td>
           
                <td>   
                    niveau : <xsl:value-of select="@niveau"/>
                </td>
            </tr>
        </table>
    </xsl:template>`

【问题讨论】:

  • 请将预期结果添加到您的问题中。

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

请尝试以下 XSLT。

XSLT

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

    <xsl:template match="/dico">
        <html>
            <head>
                <title>Dictionnaire</title>
            </head>
            <body>
                <table border="4" cellspacing="4" cellpadding="2" width="80%">
                    <thead style="background-color: #4CAF50; color: white;">
                        <tr>
                            <th>mot</th>
                            <th>niveau</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:for-each select="mot">
                            <xsl:sort select="@niveau"/>
                            <xsl:sort select="."/>
                            <tr>
                                <td>le mot est : <xsl:value-of select="."/></td>
                                <td>niveau : <xsl:value-of select="@niveau"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多