2015-07-11 20 views
0

我想要做的事情很簡單,但我似乎無法讓它工作。當文字等於某事時匹配xslt

我有以下XML:

<analysis> 
    <field> 
     <name>field_1</name> 
     <type>number</type> 
     <tag>Number Field Number One</tag> 
    </field> 
    <field> 
     <name>field_2</name> 
     <type>text</type> 
     <tag>Text Field Number One</tag> 
    </field> 
    <field> 
     <name>field_3</name> 
     <cell>A12</cell> 
     <type>Excel</type> 
     <tag>Value that comes from an Excel file</tag> 
    </field> 
</analysis> 

我想是XML輸出這樣的:

<table> 
    <thead> 
     <tr> 
      <th>Nombre</th> // Name in spanish 
      <th>Tipo</th> // Type in spanish 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Number Field Number One</td> 
      <td>Número</td> // Number in spanish 
     </tr> 
     <tr> 
      <td>Text Field Number One</td> 
      <td>Campo de Texto</td> // Text field in spanish 
     </tr> 
     <tr> 
      <td>Value that comes from an Excel file</td> 
      <td>Excel (A12)</td> // Excel (cell) 
     </tr> 
    </tbody> 
</table> 

我的改造至今是下一個:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="analysis"> 

     <table> 
      <thead> 
       <tr> 
        <th>Nombre</th> 
        <th>Tipo</th> 
       </tr> 
      </thead> 
      <tbody> 
       <xsl:apply-templates select="field[type != 'table']"/> 
       <!-- I have another type called table which I'm ignoring for this question and won't follow the same scheme --> 
      </tbody> 
     </table> 

     <br /> 

    </xsl:template> 

    <xsl:template match="campo[field != 'table']"> 

     <tr> 
      <td> 
       <xsl:value-of select="tag" /> 
      </td> 
      <td> 
       <xsl:apply-templates select="type"/> 
      </td> 
     </tr> 

    </xsl:template> 

    <!-- The following templates don't match --> 

    <xsl:template select="type = 'Excel'"> 
     <xsl:param name="cell" select="preceding-sibling::node()/cell" /> 
     <xsl:value-of select="concat('Excel ', $cell)" /> 
    </xsl:template> 

    <xsl:template select="type = 'number'"> 
     <xsl:value-of select="'Número'" /> 
    </xsl:template> 

    <xsl:template select="type = 'text'"> 
     <xsl:value-of select="'Campo de Texto'" /> 
    </xsl:template> 

</xsl:stylesheet> 

正如代碼說,最後的模板不匹配。當它的文本等於某個東西時,如何匹配標籤?我的輸出是我想要的,但是用'數字','文本'和'Excel'值而不是西班牙文中的等價物,這正是我想要的。

我試過其他東西,如<xsl:template select="field[type = 'Excel']">,但結果相同。

回答

1

「當文本與文本相等時,如何匹配標籤?」

關注的的特定問題時,它的文本等於特定值,您可以使用.它引用當前上下文元素來實現這一目標「匹配」的任務,例如相匹配的元素:

<xsl:template match="type[.='text']"> 
    <xsl:value-of select="'Campo de Texto'" /> 
</xsl:template>