I have the following xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Loci>
<Locus>
<Id>MTBC1726</Id>
<Alleles>
<Allele>
<Name>1.0</Name>
<Description>No annotation provided</Description>
</Allele>
<Allele>
<Name>2.0</Name>
<Description>No annotation provided</Description>
</Allele>
</Alleles>
</Locus>
<Locus>
<Id>MTBC3142</Id>
<Alleles>
<Allele>
<Name>1.0</Name>
<Description>No annotation provided</Description>
</Allele>
</Alleles>
</Locus>
</Loci>
And I want to create the following result:
which, in HTML, would look like this:
<html>
<body>
<table border="1">
<tr>
<th>Locus</th>
<th>Allele</th>
<th>Description</th>
</tr>
<tr>
<td rowspan="2">MTBC1726</td>
<td>1.0</td>
<td >No annotation provided</td>
</tr>
<tr>
<td>2.0</td>
<td >No annotation provided</td>
</tr>
<tr>
<td >MTBC3142</td>
<td>1.0</td>
<td >No annotation provided</td>
</tr>
</table>
</body>
</html>
I have created the following XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Locus</th>
<th>Allele</th>
<th>Description</th>
</tr>
<xsl:for-each select="Loci/Locus">
<tr>
<td><xsl:value-of select="Id"/></td>
<xsl:for-each select="Alleles/Allele">
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Description"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
but that produces a table like this:
So, my question is: how do I add the rowspan attribute with a count based on the number of <Allele> nodes?
How about this:
<table border="1">
<tr>
<th>Locus</th>
<th>Allele</th>
<th>Description</th>
</tr>
<xsl:for-each select="Loci/Locus">
<xsl:for-each select="Alleles/Allele">
<tr>
<xsl:if test="position() = 1">
<td rowspan="{last()}">
<xsl:value-of select="ancestor::Locus[1]/Id"/>
</td>
</xsl:if>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Description"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
The idea here is that within the inner for-each you can use position() to see whether you're on the first Allele for this Locus, and last() to give the number of Allele elements that the current Locus contains. The ancestor::Locus[1] is because we need to extract the Locus Id at a point where the current context is its first Allele.
If you want to avoid adding rowspan="1" for single-allele loci, you'll have to use a conditional xsl:attribute instead of an attribute value template:
<xsl:if test="position() = 1">
<td>
<xsl:if test="last() > 1">
<xsl:attribute name="rowspan">
<xsl:value-of select="last()" />
</xsl:attribute>
</xsl:if>
<xsl:value-of select="ancestor::Locus[1]/Id"/>
</td>
</xsl:if>
You could use the XSL count() function to define a variable, and then output that variable as your attribute value:
<xsl:variable name="recordCount" select="count(Alleles/Allele)"/>
<td rowspan="{$recordCount}"><xsl:value-of select="Id"/></td>
You can count the number of Allele and use that value to create your rowspan. I also put in a check so that it didn't add a rowspan=1 for cases when there was only the one Allele
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Locus</th>
<th>Allele</th>
<th>Description</th>
</tr>
<xsl:for-each select="Loci/Locus">
<tr>
<td>
<xsl:if test="count(Alleles/Allele) > 1">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(Alleles/Allele)"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="Id"/>
</td>
<xsl:for-each select="Alleles/Allele">
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Description"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Related
if we have the following xml input:
<root xmlns:ns="http://www.blabla">
<ns:element1 attribute="attr1">10</ns:element1>
<ns:element1 attribute="attr2">20</ns:element1>
<ns:element2 attribute="attr1">30</ns:element1>
<ns:element2 attribute="attr2">40</ns:element1>
</root>
how can we locate each element inside a for-each in xslt?
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Column 1</th>
<th style="text-align:left">Column 2</th>
</tr>
<xsl:for-each select="1 to 10">
<xsl:variable name="name" select="concat('element', .)"/>
<tr>
<td>
<xsl:value-of select="???what should be here???[lower-case(#attribute)='attr0']"/>
</td>
<td>
<xsl:value-of select="???same question???[lower-case(#attribute)='attr1']"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Please note that I want to follow this procedure (if possible) as the input is very dynamic and we don't always get all elements in every row.
I appreciate your help.
The expression you want is this...
<xsl:value-of select="*[local-name() = $name][lower-case(#attribute)='attr1']"/>
... Except this will fail with an error along the lines of Required item type of context item for the child axis is node(); supplied expression (.) has item type xs:integer, due to the code being executed in the context of the xsl:for-each on atomic values. To get around this, you will need to save a reference to the child elements in a variable before the xsl:for-each.
Try this XSLT
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
</tr>
<xsl:variable name="children" select="*" />
<xsl:for-each select="1 to 10">
<xsl:variable name="name" select="concat('element', .)"/>
<tr>
<td>
<xsl:value-of select="$children[local-name() = $name][lower-case(#attribute)='attr1']"/>
</td>
<td>
<xsl:value-of select="$children[local-name() = $name][lower-case(#attribute)='attr2']"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Or slightly better, to reduce code duplication...
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
</tr>
<xsl:variable name="children" select="*" />
<xsl:for-each select="1 to 10">
<xsl:variable name="name" select="concat('element', .)"/>
<xsl:variable name="element" select="$children[local-name() = $name]"/>
<tr>
<td>
<xsl:value-of select="$element[lower-case(#attribute)='attr1']"/>
</td>
<td>
<xsl:value-of select="$element[lower-case(#attribute)='attr2']"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note, I would really consider changing your input XML if you have control over it. Numbering elements using the element name makes it harder to manipulate. Ideally you would do this instead...
<ns:element num="1" attribute="attr1">10</ns:element>
If you dont have control try to take count of the elements and run the loop as per count like this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://www.blabla">
<xsl:template match="root">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Column 1</th>
<th style="text-align:left">Column 2</th>
</tr>
<xsl:variable name="all-element" select="count(//*:element)"/>
<xsl:for-each select="*[concat('ns:element', (1 to $all-element))]">
<xsl:message select="."/>
<tr>
<td>
<xsl:value-of select="#attribute"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I am trying to build an html table from an xml document using xslt. The xml-code looks like this:
<root>
<group>
<name>A</name>
<item>1</item>
<item>2</item>
<item>3</item>
</group>
<group>
<name>B</name>
<item>4</item>
<item>5</item>
</group>
</root>
The table should look like this:
<table>
<tr>
<th>Group</th>
<th>Item</th>
</tr>
<tr class="row_0">
<td>A</td>
<td>1</td>
</tr>
<tr class="row_1">
<td>.</td>
<td>2</td>
</tr>
<tr class="row_0">
<td>.</td>
<td>3</td>
</tr>
<tr class="row_1">
<td>B</td>
<td>4</td>
</tr>
<tr class="row_0">
<td>.</td>
<td>5</td>
</tr>
</table>
The problem is the alternating of the class attribute (This is needed for styling, because I don't have access to modern CSS pseudoclasses) The class should always alternate between row_0 and row_1. Because of the layout of the table, it could be expressed as something like this:
<xsl:attribute name="class">row_<xsl:value-of select="count(all previous item elements) mod 2" /></xsl:attribute>
How can I express all previous item elements as a real selector? It also has to count all item elements in previous groups. However, it should only count up to root (root in this example is not the root of my actual document)
EDIT:
My current xslt looks like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="root">
<table>
<tr>
<th>Group</th>
<th>Item</th>
</tr>
<xsl:for-each select="group">
<tr>
<xsl:attribute name="class">row_<xsl:value-of select="position() mod 2" /></xsl:attribute>
<td><xsl:value-of select="name" /></td>
<xsl:for-each select="item">
<xsl:if test="position() = 1">
<td><xsl:value-of select="text()" /></td>
</xsl:if>
</xsl:for-each>
</tr>
<xsl:for-each select="item">
<xsl:if test="position() > 1">
<tr>
<xsl:attribute name="class">row_<xsl:value-of select="position() mod 2" /></xsl:attribute>
<td>.</td>
<td><xsl:value-of select="text()" /></td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
The two attributes where it says position() mod 2 would need to be replaced.
Let me suggest a simpler approach:
<xsl:template match="/root">
<table>
<tr>
<th>Group</th>
<th>Item</th>
</tr>
<xsl:for-each select="group/item">
<tr class="row_{(position() - 1) mod 2}">
<td>
<xsl:choose>
<xsl:when test="not(preceding-sibling::item)">
<xsl:value-of select="../name" />
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
I am trying to write an XSLT template for the following XML structure:
<dealership>
<division>
<division_name>BMW</division_name>
<models>
<model_no>328i</model_no>
<model_no>M3</model_no>
<model_no>X5</model_no>
<model_no>528i</model_no>
</models>
<salesman>
<salesman_name>Bob</salesman_name>
<salesman_name>Jerry</salesman_name>
</salesman>
<mechanics>
<mechanic_name>Greg</mechanic_name>
<mechanic_name>Mike</mechanic_name>
<mechanic_name>Sean</mechanic_name>
</mechanics>
</division>
</dealership>
I need to output it to a HTML table in this format:
<table>
<tr>
<th>Division</th>
<th>Models</th>
<th>Salesman</th>
<th>Mechanics</th>
</tr>
<tr>
<td>BMW</td>
<td>328i</td>
<td>Bob</td>
<td>Greg</td>
</tr>
<tr>
<td></td>
<td>M3</td>
<td>Jerry</td>
<td>Mike</td>
</tr>
<tr>
<td></td>
<td>X5</td>
<td></td>
<td>Sean</td>
</tr>
<tr>
<td></td>
<td>528i</td>
<td></td>
<td></td>
</tr>
</table>
The problem is there can be any number of models, salesman, and mechanics. So somehow I need to get the node with the most children to know how many rows to create in the table, then I need a way to track which cells in the rows are empty. Any help would be greatly appreciated.
The best way I can think of to do it is to call a named template with the row number as a parameter. The template will then check to see if there is any data to output, and if so then build that row of the table and call itself with the next row number as a new parameter value.
This code demonstrates. The output is different from the HTML you show because yours doesn't correspond to the XML data you have given. As far as I can tell this is correct.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="html" />
<xsl:template match="/dealership/division">
<table>
<tr>
<th>Division</th>
<th>Models</th>
<th>Salesman</th>
<th>Mechanics</th>
</tr>
<xsl:call-template name="row" >
<xsl:with-param name="i" select="1" />
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="row">
<xsl:param name="i"/>
<xsl:if test="models/model_no[$i] |
salesman/salesman_name[$i] |
mechanics/mechanic_name[$i]">
<tr>
<td>
<xsl:value-of select="division_name[$i]"/>
</td>
<td>
<xsl:value-of select="models/model_no[$i]"/>
</td>
<td>
<xsl:value-of select="salesman/salesman_name[$i]"/>
</td>
<td>
<xsl:value-of select="mechanics/mechanic_name[$i]"/>
</td>
</tr>
<xsl:call-template name="row" >
<xsl:with-param name="i" select="$i + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
output
<table>
<tr>
<th>Division</th>
<th>Models</th>
<th>Salesman</th>
<th>Mechanics</th>
</tr>
<tr>
<td>BMW</td>
<td>328i</td>
<td>Bob</td>
<td>Greg</td>
</tr>
<tr>
<td></td>
<td>M3</td>
<td>Jerry</td>
<td>Mike</td>
</tr>
<tr>
<td></td>
<td>X5</td>
<td></td>
<td>Sean</td>
</tr>
</table>
Hear another possible solution.
<xsl:template match="/dealership/division">
<xsl:variable name="maxcnt">
<xsl:for-each select="*" >
<xsl:sort select="count(*)" order="descending"/>
<xsl:if test ="position()=1">
<xsl:value-of select="name(.)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<table>
<tr>
<th>Division</th>
<th>Models</th>
<th>Salesman</th>
<th>Mechanics</th>
</tr>
<xsl:apply-templates select="*[name()=$maxcnt]/*" mode="row"/>
</table>
</xsl:template>
<xsl:template match="*" mode="row">
<xsl:variable name="pos" select="count(preceding-sibling::*)+1"/>
<tr>
<td>
<xsl:value-of select="../../division_name[$pos]"/>
</td>
<td>
<xsl:value-of select="../../models/model_no[$pos]"/>
</td>
<td>
<xsl:value-of select="../../salesman/salesman_name[$pos]"/>
</td>
<td>
<xsl:value-of select="../../mechanics/mechanic_name[$pos]"/>
</td>
</tr>
</xsl:template>
The first step is to figure out which child of division has the maximum count of children by it self. This is done by an for-each over all children sorted by count of children. Therefore the first on is the one with most children.
<xsl:for-each select="*" >
<xsl:sort select="count(*)" order="descending"/>
<xsl:if test ="position()=1">
<xsl:value-of select="name(.)"/>
</xsl:if>
</xsl:for-each>
This is based on a very good explanation from Dimitre Novatchev(*)
I am creating an fo:table that has 25 columns.
When I am converting to PDF the table is too wide for the size of the page (A4), how can I break the table by column, for example, every 5 columns I want to write on another page?
Thanks
I have taken an example of table which has nine column like:
<Table>
<thead>
<tr>
<td>Head1</td>
<td>Head2</td>
<td>Head3</td>
<td>Head4</td>
<td>Head5</td>
<td>Head6</td>
<td>Head7</td>
<td>Head8</td>
<td>Head9</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
<td>2-6</td>
<td>2-7</td>
<td>2-8</td>
<td>2-9</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
<td>3-4</td>
<td>3-5</td>
<td>3-6</td>
<td>3-7</td>
<td>3-8</td>
<td>3-9</td>
</tr>
</tbody>
</Table>
Now, I have created XSLT to break it into multiple table by passing integer value in <xsl:param name="columnNumber"/> param:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:param name="columnNumber" select="xs:integer(4)"/>
<xsl:template match="Table">
<xsl:param name="countColumn" select="count(//tbody/tr[1]/td)"/>
<xsl:choose>
<xsl:when test="$countColumn gt $columnNumber">
<xsl:call-template name="divideTable">
<xsl:with-param name="tableToGenerate" select="ceiling($countColumn div $columnNumber)"/>
<xsl:with-param name="orginialTable" select="self::*"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="divideTable">
<xsl:param name="tableToGenerate"/>
<xsl:param name="orginialTable"/>
<xsl:param name="start" select="xs:integer(1)"/>
<xsl:param name="end" select="$columnNumber"/>
<xsl:param name="counter" select="xs:integer(1)"/>
<xsl:choose>
<xsl:when test="$counter le $tableToGenerate">
<Table>
<thead>
<xsl:copy-of
select="$orginialTable/thead/tr/td[position() le $end and position() ge $start]"/>
</thead>
<tbody>
<xsl:for-each select="$orginialTable/tbody/tr">
<tr>
<xsl:copy-of select="td[position() le $end and position() ge $start]"/>
</tr>
</xsl:for-each>
</tbody>
</Table>
<xsl:call-template name="divideTable">
<xsl:with-param name="start" select="xs:integer(($columnNumber * $counter) + 1)"/>
<xsl:with-param name="end" select="xs:integer(($columnNumber * $counter) + $columnNumber)"/>
<xsl:with-param name="tableToGenerate" select="$tableToGenerate"/>
<xsl:with-param name="orginialTable" select="$orginialTable"/>
<xsl:with-param name="counter" select="xs:integer($counter + 1)"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It gives me ouput:
<?xml version="1.0" encoding="UTF-8"?>
<Table xmlns:xs="http://www.w3.org/2001/XMLSchema">
<thead>
<td>Head1</td>
<td>Head2</td>
<td>Head3</td>
<td>Head4</td>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
<td>3-4</td>
</tr>
</tbody>
</Table>
<Table xmlns:xs="http://www.w3.org/2001/XMLSchema">
<thead>
<td>Head5</td>
<td>Head6</td>
<td>Head7</td>
<td>Head8</td>
</thead>
<tbody>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>2-5</td>
<td>2-6</td>
<td>2-7</td>
<td>2-8</td>
</tr>
<tr>
<td>3-5</td>
<td>3-6</td>
<td>3-7</td>
<td>3-8</td>
</tr>
</tbody>
</Table>
<Table xmlns:xs="http://www.w3.org/2001/XMLSchema">
<thead>
<td>Head9</td>
</thead>
<tbody>
<tr>
<td>9</td>
</tr>
<tr>
<td>2-9</td>
</tr>
<tr>
<td>3-9</td>
</tr>
</tbody>
</Table>
Please have a look on this and check if this example helps
My solution was creating two independent tables.
Then I created two block of xsl templates, to differentiate the first block of the second block I used mode function.
Like this:
Table
....
<xsl:apply-templates select="content[#teste='1']" mode="first" />
....
<xsl:apply-templates select="content[#teste='1']" mode="second" />
....
XSL Templates block
....
<xsl:template match="content[#teste='1']" mode="first">
....
<xsl:template match="content[#teste='1']" mode="second">
....
I have the following piece of XSLT code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:variable name="condition">(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')</xsl:variable>
<xsl:template match="cia">
<html>
<head></head>
<body>
<table border="1">
<tr>
<th>Country</th>
<th>Capital</th>
<th>Area</th>
<th>Population</th>
<th>Inflation rate</th>
</tr>
<xsl:for-each select="country">
<xsl:if test="{$condition}">
<tr>
<td>
<xsl:value-of select="#name"/>
</td>
<td>
<xsl:value-of select="#capital"/>
</td>
<td>
<xsl:value-of select="#total_area"/>
</td>
<td>
<xsl:value-of select="#population"/>
</td>
<td>
<xsl:value-of select="#inflation"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If put the conditional expression directly in the if element the code works fine, however, my problem is that when I assign the same conditional expression to the variable and then reference it in the if element it doesn't work. Am I doing something wrong? Is this possible?
Thanks!
There are multiple problems:
The brackets in <xsl:if test="{$condition}"> are unnecessary; use <xsl:if test="$condition">
Use the following xsl:variable construct:
<xsl:variable name="condition"
select="(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')"/>
When you had the condition in your xsl:if the test was performed relative to each country. This is not the case in a top-level variable. The value of the variable is the result of the expression, not the expression itself. If you insist on a variable, then initialize it inside the loop.
See the following stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="cia">
<html>
<head></head>
<body>
<table border="1">
<tr>
<th>Country</th>
<th>Capital</th>
<th>Area</th>
<th>Population</th>
<th>Inflation rate</th>
</tr>
<xsl:for-each select="country">
<xsl:variable name="condition"
select="(coasts='Adriatic Sea') or
(coasts='Mediterranean Sea')" />
<xsl:if test="$condition">
<tr>
<td>
<xsl:value-of select="#name" />
</td>
<td>
<xsl:value-of select="#capital" />
</td>
<td>
<xsl:value-of select="#total_area" />
</td>
<td>
<xsl:value-of select="#population" />
</td>
<td>
<xsl:value-of select="#inflation" />
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Input:
<cia>
<country name="test1" inflation="22">
<coasts>Adriatic Sea</coasts>
</country>
<country name="test2" inflation="7">
<coasts>No match</coasts>
</country>
</cia>
Output (only the first country passes the test):
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table border="1">
<tr>
<th>Country</th>
<th>Capital</th>
<th>Area</th>
<th>Population</th>
<th>Inflation rate</th>
</tr>
<tr>
<td>test1</td>
<td></td>
<td></td>
<td></td>
<td>22</td>
</tr>
</table>
</body>
</html>
Note that you don't really even need a separate condition; it's better to select just the desired elements in the first place. This loop produces the same output:
<xsl:for-each
select="country[(coasts='Adriatic Sea') or
coasts='Mediterranean Sea')]">
<tr>
<td>
<xsl:value-of select="#name" />
</td>
<td>
<xsl:value-of select="#capital" />
</td>
<td>
<xsl:value-of select="#total_area" />
</td>
<td>
<xsl:value-of select="#population" />
</td>
<td>
<xsl:value-of select="#inflation" />
</td>
</tr>
</xsl:for-each>
Replace:
<xsl:for-each select="country">
with
<xsl:apply templates select="country"/>
also, add these templates:
<xsl:template match="country"/>
<xsl:template match=
"country[coasts='Adriatic Sea'
or
coasts='Mediterranean Sea'
]">
<xsl:template match=
"country[coasts='Adriatic Sea'
or
coasts='Mediterranean Sea'
]">
<tr>
<td>
<xsl:value-of select="#name"/>
</td>
<td>
<xsl:value-of select="#capital"/>
</td>
<td>
<xsl:value-of select="#total_area"/>
</td>
<td>
<xsl:value-of select="#population"/>
</td>
<td>
<xsl:value-of select="#inflation"/>
</td>
</tr>
</xsl:template>
Did you note that the <xsl:if> "magically" disappeared?
Of course, this code can be improved even further, but you haven't provided neither an instance of the XML document on which the transformation is to be applied, nor the desired output.