krumpli
krumpli

Reputation: 742

XSLT Sorting a table with information from external documents

I have major problems with this task and i can't figure out how to do the sorting.

I'm trying to sort a table in XSLT where I'm importing an .XSL. In this .XSL i have two external .XSL referenced. Output should be 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/letters/letter">
                            <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"?>

        <letters>
            <letter>A</letter>
            <letter>C</letter>
            <letter>B</letter>
            <letter>E</letter>
            <letter>D</letter>
        </letters>

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>

So the letters in fileA.xml are attatched to an animal on the same row in fileB.xml

What i get now is a table:

A - dog

B - horse

C - cow

D - snake

E - spider

What i want to get is:

A - dog

B - cow

C - horse

D - spider

E - snake

I can't figure out how to sort the columns together , only column_1, after the for-each loop. Tried to find similar problems here but to no avail. I posted a similar question before and got a correct answer but i forgot to edit the numbers to letters. Having numbers makes it easier when using position(). I assume that in this case position() could be used as an index but it's a long shot. I believe this has a simpler solution.

Upvotes: 1

Views: 93

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

Change

<xsl:variable name="current_node" select="position()"/>

to

<xsl:variable name="orig-pos"><xsl:number/></xsl:variable>

and then use

<xsl:value-of select="$fileB//animal[position() = $orig-pos]" />

Upvotes: 2

Related Questions