Reputation: 423
What I'm trying to do is really simple, but I can't seem to get it working.
I have the following 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>
I want that XML to output this:
<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>
My transformation so far is the next:
<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>
As the code says, the last templates don't match. How do I match a tag when its text is equals to something? My output is what I want, but with 'number', 'text' and 'Excel' values instead of their equivalents in spanish, which is what I want.
I've tried other things like <xsl:template select="field[type = 'Excel']">
, but same result.
Upvotes: 1
Views: 2787
Reputation: 1828
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:template match="*:analysis">
<table>
<thead>
<tr>
<th>Nombre</th>
<th>Tipo</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="node()|@*"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="*:field">
<tr>
<td><xsl:value-of select="tag"/></td>
<td><xsl:apply-templates select="*:type"/><xsl:apply-templates select="*:cell"/></td>
</tr>
</xsl:template>
<xsl:template match="*:type[text()='text']">Campo de Texto</xsl:template>
<xsl:template match="*:type[text()='number']">Número</xsl:template>
<xsl:template match="*:type[text()='Excel']">Excel</xsl:template>
<xsl:template match="*:cell"> (<xsl:value-of select="."/>)</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 89285
"How do I match a tag when its text is equals to something?"
Focusing on the specific problem of matching an element when it's text equals specific value, you can use .
which references current context element to accomplish that 'matching' task, for example :
<xsl:template match="type[.='text']">
<xsl:value-of select="'Campo de Texto'" />
</xsl:template>
Upvotes: 3