Xsl

0 respostas
ECO2004

Estou com um problema com um xls…

Eu quero fazer o XML funcionar pelo XSL 2, mas está imprimindo a tabela três vezes, pois há três elementos professor. Como que eu poderia fazer usando apply-templates, como no XSL 2 e manter a impressão da XSL 1?

XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type = "text/xsl" href = "escola.xsl"?>
<es:escola xmlns:es = "ESCOLA-NAMESPACE"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation = "ESCOLA-NAMESPACE escola.xsd">
	<diretor>
	
		<professor nome = "João" disciplina = "Inglês">
			<aluno sexo  = "M" serie = "8">Carlos</aluno>
			<aluno sexo  = "F" serie = "8">Maria</aluno>
			<aluno sexo  = "M" serie = "8">Sérgio</aluno>
			<aluno sexo  = "M" serie = "8">Nícolas</aluno>
		</professor>
		
		<professor nome = "Carla" disciplina = "Português">
			<aluno sexo  = "M" serie = "8">Carlos</aluno>
			<aluno sexo  = "F" serie = "8">Maria</aluno>
			<aluno sexo  = "M" serie = "8">Sérgio</aluno>
			<aluno sexo  = "M" serie = "8">Nícolas</aluno>
		</professor>
		
		<professor nome = "Pedro" disciplina = "Matemática">
			<aluno sexo  = "M" serie = "8">Carlos</aluno>
			<aluno sexo  = "F" serie = "8">Maria</aluno>
			<aluno sexo  = "M" serie = "8">Sérgio</aluno>
			<aluno sexo  = "M" serie = "8">Nícolas</aluno>
		</professor>
		
		<secretaria turno = "manha">Maria</secretaria>
		<secretaria turno = "tarde">Fátima</secretaria>
		
	</diretor>
</es:escola>

XSL 1

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "html" omit-xml-declaration = "no"
	doctype-system = "www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/>
	
	<!-- The <xsl:template> element contains rules to apply when a specified node is matched
	The match attribute is used to associate the template with an XML element. The match attribute
	can also be used to define a template for a whole branch of the XML document
	(i.e. match="/" defines the whole document). -->
	
	<xsl:template match = "/">
		<html xmlns = "http://www.w3.org/1999/xhtml">
			<head>
				<title>Escola</title>
			</head>
			
			<body><xsl:apply-templates/></body>
		</html>
	</xsl:template>
	
	<xsl:template match = "diretor">
		<table border = "1">
			<thead>
				<tr>
					<td><strong>Professor</strong></td>
					<td><strong>Disciplina</strong></td>
				</tr>				
			</thead>
			
			<tbody>
				<xsl:for-each select = "//professor">
					<tr>
						<td>
							<xsl:value-of select = "@nome"/>
						</td>
						
						<td>
							<xsl:value-of select = "@disciplina"/>
						</td>				
					</tr>
				</xsl:for-each>
			</tbody>
			
		</table>
	</xsl:template>
	
</xsl:stylesheet>

XSL 2

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "html" omit-xml-declaration = "no"
	doctype-system = "www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"/>
	
	<!-- The <xsl:template> element contains rules to apply when a specified node is matched
	The match attribute is used to associate the template with an XML element. The match attribute
	can also be used to define a template for a whole branch of the XML document
	(i.e. match="/" defines the whole document). -->
	
	<xsl:template match = "/">
		<html xmlns = "http://www.w3.org/1999/xhtml">
			<head>
				<title>Escola</title>
			</head>
			
			<body><xsl:apply-templates/></body>
		</html>
	</xsl:template>
	
	<xsl:template match = "diretor">
		<table border = "1">
			<thead>
				<tr>
					<td><strong>Professor</strong></td>
					<td><strong>Disciplina</strong></td>
				</tr>				
			</thead>
			
			<tbody>
				<xsl:apply-templates select = "professor"/>	
			</tbody>
			
		</table>
	</xsl:template>

	<xsl:template match = "professor">		

		<xsl:for-each select = "//professor">
			<tr>
				<td>
					<xsl:value-of select = "@nome"/>
				</td>
				
				<td>
					<xsl:value-of select = "@disciplina"/>
				</td>				
			</tr>
		</xsl:for-each>
		
	</xsl:template>
</xsl:stylesheet>
Criado 20 de outubro de 2011
Respostas 0
Participantes 1