XSLT使用外部文档中的信息对表进行排序

时间:2017-10-12 19:53:50

标签: xml sorting xslt

我遇到了这个任务的主要问题,我无法弄清楚如何进行排序。

我正在尝试在XSLT中对表进行排序,我正在导入.XSL。在这个.XSL中我引用了两个外部.XSL。输出应该是html。

mainXSL.xsl

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

                <xsl:variable name="fileA" select="document(/importFiles/docs/@fileA)" />
                <xsl:variable name="fileB" select="document(/importFiles/docs/@fileB)" />
    <xsl:template match="/">
        <html>
            <head>
                <title> 
                    Task1
                </title>
            </head>        
                <body>
                    <table align="center" border="1">
                        <tr>
                            <th>column_1</th>
                            <th>column_2</th>
                        </tr>

                        <xsl:for-each select="$fileA/numbers/number">
                            <xsl:sort select="." order="ascending"/>
                                <xsl:variable name="current_node" select="position()"/>
                                    <tr>                                  
                                        <td class="_fileA"><xsl:value-of select="." /></td>
                                        <td class="_fileB"><xsl:value-of select="$fileB//animal[$current_node]" /></td>
                                    </tr>
                            </xsl:for-each>
                    </body>
               </html> 
</xsl:template>

INDEX.XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mainXSL.xsl"?>

<importFiles>
    <docs fileA = "fileA.xml" />
    <docs fileB = "fileB.xml" /> 
</importFiles>

fileA.xml

<?xml version="1.0" encoding="UTF-8"?>

        <numbers>
            <number>A</number>
            <number>C</number>
            <number>B</number>
            <number>E</number>
            <number>D</number>
        </numbers>

fileB.xml

<?xml version="1.0" encoding="UTF-8"?>

        <animals>
            <animal>dog</animal>
            <animal>horse</animal>
            <animal>cow</animal>
            <animal>snake</animal>
            <animal>spider</animal>
        </animals>

因此fileA.xml中的数字被附加到fileB.xml中同一行的动物

我现在得到的是一张桌子:

1 - 狗

2 - 马

3 - 牛

4 - 蛇

5 - 蜘蛛

我想得到的是:

1 - 狗

2 - 牛

3 - 马

4 - 蜘蛛

5 - 蛇

我无法弄清楚如何在for-each循环之后将列排序在一起,只有column_1。 试图在这里找到类似的问题但无济于事。 PS。抱歉格式化,不确定缩进是否正确。

1 个答案:

答案 0 :(得分:0)

我认为您不想要<xsl:sort select="." order="ascending"/>并将其删除,然后您可以将<td class="_fileA"><xsl:value-of select="position()" /></td>用于第一列,而将第二列用于<td class="_fileB"><xsl:value-of select="$fileB//animal[position() = current()]" /></td>

作为完整的样式表我得到了

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

    <xsl:variable name="fileA" select="document(/importFiles/docs/@fileA)" />
    <xsl:variable name="fileB" select="document(/importFiles/docs/@fileB)" />
    <xsl:template match="/">
        <html>
            <head>
                <title> 
                    Task1
                </title>
            </head>        
            <body>
                <table align="center" border="1">
                    <tr>
                        <th>column_1</th>
                        <th>column_2</th>
                    </tr>

                    <xsl:for-each select="$fileA/numbers/number">

                        <tr>                                  
                            <td class="_fileA"><xsl:value-of select="position()" /></td>
                            <td class="_fileB"><xsl:value-of select="$fileB//animal[position() = current()]" /></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html> 
    </xsl:template>
</xsl:stylesheet>

这给了我输出

column_1    column_2
1   dog
2   cow
3   horse
4   spider
5   snake

https://martin-honnen.github.io/xslt/2017/test201710120301.xml在线。