简体   繁体   中英

How to determine the order of table columns in xslt?

From the following xml:

<?xml version="1.0"?>     
<lieferungen>
  <artikel id="3526">
    <name>apfel</name>
    <preis stueckpreis="true">8.97</preis>
    <lieferant>Fa. Krause</lieferant>
  </artikel>
  <artikel id="7866">
    <name>Kirschen</name>
    <preis stueckpreis="false">10.45</preis>
    <lieferant>Fa. Helbig</lieferant>
  </artikel>
  <artikel id="3526">
    <name>apfel</name>
    <preis stueckpreis="true">12.67</preis>
    <lieferant>Fa. Liebig</lieferant>
  </artikel> 
  <artikel id="7789">
    <name>Ananas</name>
    <preis stueckpreis="true">8.60</preis>
    <lieferant>Fa. Richard</lieferant>
  </artikel>
</lieferungen>

I want to create a table that looks as follows:

四列表

To this end I wrote the following xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="lieferungen">
        <html>
            <head>
                <title>
                    <xsl:text>Lieferungen</xsl:text>
                </title>
            </head>
            <body bgcolor="#ffffff">
                <h1>
                    Lieferungen (Deliveries)
                </h1>
                <hr/>
                <table border="1">
                    <tr>
                        <th>Nummer</th>
                        <th>Article</th>
                        <th>Price</th>
                        <th>Supplier</th>
                    </tr>
                    <xsl:apply-templates/>             
                </table>
            </body>
            <hr/>
            <p>

            </p>
        </html>
    </xsl:template>
    <xsl:template match="artikel">
        <tr>
            <td>
                <xsl:value-of select="@id"/>
            </td>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

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

     <xsl:template match="preis">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

    <xsl:template match="lieferant">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>


</xsl:stylesheet>

The code worked fine and I got my table ... however, now I want to switch the columns, specifically I want to switch columns 3 and 4. To this end, I simply switch the order of the template for "preis" and "lieferant", ie the new order is now:

    <xsl:template match="lieferant">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

    <xsl:template match="preis">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

The rest of the code is the same. This approach did not work out though and the order of the columns in the table stayed the same.

My question is therefore: How can I make the computer use

  <xsl:template match="lieferant">

in the third and

   <xsl:template match="preis">

for the fourth column of the table?

The order in which the templates appear in the stylesheet has no significance (except when resolving conflicts). In order to switch the columns, change this:

<xsl:template match="artikel">
    <tr>
        <td>
            <xsl:value-of select="@id"/>
        </td>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

to :

<xsl:template match="artikel">
    <tr>
        <td>
            <xsl:value-of select="@id"/>
        </td>
        <xsl:apply-templates select="name, lieferant, preis"/>
    </tr>
</xsl:template>

Don't forget to switch the columns labels, too.


Note also that you can combine your last three templates into one as:

<xsl:template match="name | preis | lieferant">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

and even shorten the whole block to just:

<xsl:template match="artikel">
    <tr>
        <xsl:apply-templates select="@id, name, lieferant, preis"/>
    </tr>
</xsl:template>

<xsl:template match="@id | name | preis | lieferant">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

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