2017-01-25 3 views
1

Со следующего XML:Как определить порядок столбцов таблицы в xslt?

<?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> 

Я хочу, чтобы создать таблицу, которая выглядит следующим образом:

table with four columns

С этой целью я написал следующее 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> 

Код работал нормально, и я получил свою таблицу ... однако теперь я хочу переключать столбцы, в частности, я хочу переключать столбцы 3 д 4. С этой целью, я просто изменить порядок шаблона для «Preis» и «lieferant», т.е. новый порядок теперь:

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

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

Остальная часть кода является то же самое. Такой подход не сработал, и порядок столбцов в таблице остался прежним.

Мой вопрос таким образом: Как я могу сделать использование компьютера

<xsl:template match="lieferant"> 

в третьей и

<xsl:template match="preis"> 

для четвертой колонке таблицы?

ответ

2

Порядок, в котором шаблоны отображаются в таблице стилей, не имеет значения (кроме случаев, когда разрешаются конфликты). Для того, чтобы переключить столбцы, изменить:

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

к:

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

Не забудьте переключить колонки этикетки тоже.


Заметим также, что вы можете объединить последние три шаблона в один, как:

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

и даже сократить весь блок просто:

<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> 
Смежные вопросы