You are on page 1of 69

XSL(T)

XML Stylesheet Language


(Transformation)

Isidro Vila Verde


Tipo de Linguagem
O XSL uma linguagem de Programao
Declarativa. Por oposio:
 O PHP uma linguagem de Programao
Imperativa
 O HTML uma linguagem de anotao com
hiperligaes (hypertlinks)
 O XML uma linguagem, extensvel, de anotao
de documentos

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 2
Como funciona
 Um programa XSL usado por um
Processador de XSLT para transformar
um dado documento XML
 Quer o XML quer o XSL so documentos
recebidos pelo Processar
 O resultado outro ficheiro XML com uma
nova estrutura e cujo contedo, ou parte
deste, pode ter sido ou no
alterado/filtrado/substitudo
o Em grande parte das vezes o resultado um XML com
marcas (tags
(tags)) do HTML 4.0, ou seja um xHTML
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 3
XSLT Processor
Saxon XML
Xalan xhtml
XML Sablotron XSL-FO
IE Text*
FF

Processador
XML
XSLT

XSLT

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 4
Resultado da Transformao
o Em situaes especiais o resultado pode ser um HTML
ou um documento de texto ou mesmo um RTF
o Pode ainda ser um XSL-
XSL-FO que pode posteriormente
ser transformado por um processador de XSL-
XSL-FO (FOP
por exemplo) em PDF ou em Postscript
o Em circunstncias muito especiais pode ser um novo
XSL. Estamos neste caso em presena de um XSL de 2
gerao.
o Na maioria das vezes um XML com uma estrutura
diferente mantendo o mesmo contedo. usado em
casos onde necessrio converter uma estrutura numa
outra.
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 5
Ns
 Do ponto de vista do Processador XSL
tudo so Ns
 Cada documento XML tem um N Raiz, cujo
contedo o documento inteiro
o Debaixo deste temos as instrues de
processamento (se presentes) e o N Elemento
Raiz
 O N Elemento Raiz contm todos os outros
Ns Elemento, Ns Atributo e Ns texto
o Um documento XML bem formado s pode ter um
N Elemento Raiz
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 6
Tipos de Ns
<?xml version="1.0" encoding="UTF-8"?>
N Elemento <?xml-stylesheet type="text/xsl" href="ex1.xsl"?> N Instruo de
Raiz <curso nome="LCI"> Processamento
<disciplina nome="II" semestre="1">
<docente>
<nome>Joo</nome>
<email>j@fe.up.pt</email> N Atributo
</docente>
<alunos>
<aluno> N Texto
N Elemento <nome>Ana</nome>
<email>a@fe.up.pt</email>
</aluno>
<aluno>
<nome>Rita</nome>
<email>r@fe.up.pt</email>
</aluno>
<aluno/>
</alunos>
</disciplina>
</curso>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 7
Estrutura em rvore e Tipos de Ns
N Raiz
N Elemento
Curso Raiz N
Elemento

nome=LCI disciplina

nome=II
semestre=1 alunos
N Atributo N Texto

docente aluno aluno

nome email nome email nome email

Joo j@fe.up.pt Ana a@fe.up.pt Rita r@fe.up.pt

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 8
Elemento Raiz de um XSLT
ex1.xsl

<?xml version="1.0" encoding="UTF-8"?>


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

O elemento Raiz de um XSL(T) a marca


stylesheet no espao de nomes
http://www.w3.org/1999/XSL/Transform
http://www.w3.org/1999/XSL/ Transform
usa-se o prefixo xsl
o Habitualmente usa-
o O prefixo associado ao espao de nomes, tal como
em qualquer outro documento XML, atravs da
declarao xmlns
Se preferisse usar o prefixo x teria de fazer
xmlns:x = http://www.w3.org/1999/XSL/
http://www.w3.org/1999/XSL/Transform
Transform
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 9
Resultado da Transformao 1
<?xml version="1.0" encoding="UTF-8"?> ex1.xml
<?xml-stylesheet type="text/xsl" href="ex1.xsl"?> Instruo de
<curso nome="LCI"> Processamento
<disciplina nome="II" semestre="1">
<docente> (Diz ao
<nome>Joo</nome> Processador de
<email>j@fe.up.pt</email> XSL onde pode
</docente> encontrar o
<alunos> XSLT para
<aluno> transformar
<nome>Ana</nome> este
<email>a@fe.up.pt</email> documento)
</aluno>
<aluno>
<nome>Rita</nome>
<email>r@fe.up.pt</email>
</aluno> Result
<aluno/> <?xml version="1.0" encoding="UTF-8"?>
</alunos></disciplina></curso> Jooj@fe.u.ptAnaa@fe.up.ptRitar@fe.up.pt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
ex1.xsl

</xsl:stylesheet>
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 10
Resultado da Transformao 1
Curso

nome=LCI disciplina

nome=II XALAN
semestre=1 alunos

docente aluno aluno

nome email nome email nome email

a@fe.up r@fe.up
Joo j@fe.up.pt Ana Rita
.pt .pt

ex1.xsl

<?xml version="1.0" encoding="UTF-8"?>


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

Result
<?xml version="1.0" encoding="UTF-8"?>
Jooj@fe.u.ptAnaa@fe.up.ptRitar@fe.up.pt

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 11
Anlise do resultado
 Como o XSLT s tem o elemento raiz o
Processador aplica as regras por omisso
 De uma forma muito simplista pode-
pode-se dizer que:
o a regra por omisso : cada N Elemento
transformado no seu contedo de texto
o Os Ns Atributo so ignorados
 Adiante reformularemos esta definio

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 12
Definio de Regras
<?xml version="1.0" encoding="UTF-8"?>
ex2.xsl

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


<xsl:template match="nome">
<div style="margin:1px">
aaaaaaaaaaa
</div>
</xsl:template>
</xsl:stylesheet>
 As regras so definidas para um n, ou um
conjunto de ns, atravs do elemento
template
 No atributo match deste elemento especifica-
especifica-se
uma expresso Xpath que identifica o, ou os,
N(s) ao(s) qual/quais a regra pode ser aplicada
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 13
Definio de Regras
 Os Ns cuja regra se aplique so
Transformados/Substituidos
Transformados/ Substituidos pelo conteudo
do template
 No exemplo anterior, os Ns Elemento cujo nome
seja nome so substitudos pelo contedo de
template <div style="margin:1px">
aaaaaaaaaaa
</div>

 Todos os outros Ns sero transformados de


acordo com as regras omissas para cada tipo de
N
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 14
Definio de Regras
<?xml version="1.0" encoding="UTF-8"?>
ex2.xsl

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


<xsl:template match="nome">
<div style="margin:1px"> <?xml version="1.0" encoding="UTF-8"?>
aaaaaaaaaaa <?xml-stylesheet type="text/xsl"
</div> href="ex2.xsl"?>
</xsl:template> <curso nome="LCI">
</xsl:stylesheet> <disciplina nome="II" semestre="1">
<docente>
<nome>Joo</nome>
<?xml version="1.0" encoding="UTF-8"?> <email>j@fe.up.pt</email>
Resultado

<div style="margin:1px"> </docente>


aaaaaaaaaaa <alunos>
<aluno>
</div>j@fe.up.pt<div style="margin:1px">
<nome>Ana</nome>
aaaaaaaaaaa <email>a@fe.up.pt</email>
</div>a@fe.up.pt<div style="margin:1px"> </aluno>
aaaaaaaaaaa <aluno>
</div>r@fe.up.pt <nome>Rita</nome>
<email>r@fe.up.pt</email>
</aluno></alunos></disciplina></curso>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 15
Obteno do valor de um N
<?xml version="1.0" encoding="UTF-8"?>
ex2.xsl

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


<xsl:template match="nome">
<div style="margin:1px">
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
 Para obter o valor de um n usa-
usa-se o
elemento value
value-
-of com o atributo select
contendo uma expresso Xpath para
seleccionar o N pretendido
o Se a expresso Xpath for apenas um ponto '.' o N
seleccionado o prprio N
Outras possibilidades sero vistas adiante
http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 16
08
08-
-11
11-
-10
Obteno do valor de um N
<?xml version="1.0" encoding="UTF-8"?>
ex3.xsl

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


<xsl:template match="nome">
<div style="margin:1px">
<xsl:value-of select="."/> <?xml version="1.0" encoding="UTF-8"?>
</div> <?xml-stylesheet type="text/xsl"
</xsl:template> href="ex3.xsl"?>
</xsl:stylesheet> <curso nome="LCI">
<disciplina nome="II" semestre="1">
<docente>
<nome>Joo</nome>
Resultado

<?xml version="1.0" encoding="UTF-8"?> <email>j@fe.up.pt</email>


<div style="margin:1px">Joo</div>j@fe.up.pt </docente>
<div style="margin:1px">Ana</div>a@fe.up.pt <alunos>
<aluno>
<div style="margin:1px">Rita</div>r@fe.up.pt
<nome>Ana</nome>
<email>a@fe.up.pt</email>
</aluno>
<aluno>
<nome>Rita</nome>
<email>r@fe.up.pt</email>
</aluno></alunos></disciplina>
</curso>
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 17
Elemento Raiz do Resultado
<?xml version="1.0" encoding="UTF-8"?>
ex4.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Boa prtica:
<xsl:template match="nome">
<div style="margin:1px"> O N Elemento
<xsl:value-of select="."/>
</div> Raiz (ou,
</xsl:template>
<xsl:template match=/curso">
alternativamente,
<html>
<head>
o N Raiz) deve
<title>ex4</title> ser transformado
</head>
<body>Curso</body> no N Elemento
</html>
</xsl:template> Raiz do Resultado
</xsl:stylesheet>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 18
Anteviso de um Problema
 Neste ltimo exemplo o N Elemento Raiz
(curso) vai ser transformado em
<html>
<head>
<title>ex4</title>
</head>
<body>Curso</body>
</html>

 o que diz a regra que se aplica a esse elemento


o O Processador de XSLT faz exactamente o que lhe
dito (nada mais) e o resultado .... (ver prximo slide)

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 19
Problema!?
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
ex4.xsl

<xsl:stylesheet version="1.0" <?xml-stylesheet type="text/xsl"


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">href="ex4.xsl"?>
<xsl:template match="nome"> <curso nome="LCI">
<div style="margin:1px"> <disciplina nome="II" semestre="1">
<xsl:value-of select="."/> <docente>
</div> <nome>Joo</nome>
<email>j@fe.up.pt</email>
</xsl:template>
</docente>
<xsl:template match="curso"> <alunos>
<html> <aluno>
<head> <nome>Ana</nome>
<title>ex4</title> <email>a@fe.up.pt</email>
</head> </aluno>
<body>Curso</body> <aluno>
</html> <nome>Rita</nome>
<html> <email>r@fe.up.pt</email>
Resultado

</xsl:template>
</aluno></alunos></disciplina>
</xsl:stylesheet> <head>
<title>ex4</title> </curso>
</head>
<body>Curso</body>
</html>
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 20
Modus operandis
O Processador de XSL(T) comea por
transformar o N Raiz do documento XML
 Transforma
Transforma--o de acordo com a regra definida
o Caso no exista usa a regra omissa
A regra omisso para o N Raiz e para qualquer N
Elemento : aplicar Regras aos Ns Filho

 Se a regra definida no mandar explicitamente


aplicar regras aos Ns Filho estes so
ignorados e no influenciam o resultado
 O mesmo se passa com qualquer N Elemento
quando o processador avana para esse N
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 21
Omisso de sub-rvores
<?xml version="1.0" encoding="UTF-8"?>
ex5.xsl

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<?xml-stylesheet type="text/xsl"
<xsl:template match="nome"> href="ex5.xsl"?>
<div style="margin:1px"> <curso nome="LCI">
<xsl:value-of select="."/> <disciplina nome="II" semestre="1">
</div> <docente>
</xsl:template> <nome>Joo</nome>
<email>j@fe.up.pt</email>
<xsl:template match="docente">
</docente>
<!-- os eventuais Ns Docente sero ignorados <alunos>
bem como todos os seus Ns descendentes--> <aluno>
</xsl:template> <nome>Ana</nome>
</xsl:stylesheet> <email>a@fe.up.pt</email>
</aluno>
<?xml version="1.0" encoding="UTF-8"?> <aluno>
Resultado

<div style="margin:1px">Ana</div>a@fe.up.pt <nome>Rita</nome>


<email>r@fe.up.pt</email>
<div style="margin:1px">Rita</div>r@fe.up.pt
</aluno></alunos></disciplina></curso>

 Como se pode observar, no resultado, os dados da


sub--rvore docente foram suprimidos
sub
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 22
Aplicao de Regras a Ns Filho
<?xml version="1.0" encoding="UTF-8"?>
ex6.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="nome">
<div style="margin:1px">  iii
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="curso">
<html>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 23
Aplicao de Regras a Ns Filho
Para explicitamente aplicar regras aos Ns Elemento Filho usa-
se o elemento apply-template com o atributo select contendo
uma expresso Xpath ('*, neste caso) que selecciona todos os
Ns Elemento Filho

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 24
Aplicao de Regras a Ns Filho
<?xml version="1.0" encoding="UTF-8"?>
ex6.xsl

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0" <?xml-stylesheet type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> href="ex6.xsl"?>
<xsl:template match="nome"> <curso nome="LCI">
<div style="margin:1px"> <disciplina nome="II" semestre="1">
<xsl:value-of select="."/> <docente>
</div> <nome>Joo</nome>
<email>j@fe.up.pt</email>
</xsl:template>
</docente>
<xsl:template match="curso"> <alunos>
<html> <aluno>
<body> <nome>Ana</nome>
<xsl:apply-templates select="*"/> <email>a@fe.up.pt</email>
</body> </aluno>
</html> <aluno>
</xsl:template> Resultado <nome>Rita</nome>
<html> <email>r@fe.up.pt</email>
</xsl:stylesheet>
<body> </aluno></alunos></disciplina>
</curso>
<div style="margin:1px">Joo</div>j@fe.up.pt
<div style="margin:1px">Ana</div>a@fe.up.pt
<div style="margin:1px">Rita</div>r@fe.up.pt</body>
</html>
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 25
Regras partilhadas
<?xml version="1.0" encoding="UTF-8"?>
ex7.xsl

<xsl:stylesheet version="1.0"  Nesta caso


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="nome|email">
<div>
nome e email
<xsl:value-of select="."/>
</div> partilham a
</xsl:template>
<xsl:template match="curso"> mesma regra
<html>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 26
Obteno do nome do N
<?xml version="1.0" encoding="UTF-8"?>
ex8.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 A funo
<xsl:template match="nome|email">
<div>
name()
name()
<xsl:value-of select="name()"/>:<xsl:value-of select="."/>
</div> permite obter
</xsl:template>
<xsl:template match="curso"> o nome do n
<html>
<body> corrente
<xsl:apply-templates select="*"/>
</body>
</html>  uma funo
</xsl:template>
</xsl:stylesheet> Xpath

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 27
Obteno do nome do N
<?xml version="1.0" encoding="UTF-8"?>
ex8.xsl

<?xml version="1.0" encoding="UTF-


<xsl:stylesheet version="1.0" <?xml-stylesheet type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> href="ex8.xsl"?>
<xsl:template match="nome|email"> <curso nome="LCI">
<div> <disciplina nome="II" semestre="1">
<xsl:value-of select="name()"/>:<xsl:value-of select="."/> <docente>
</div> <nome>Joo</nome>
</xsl:template> <email>j@fe.up.pt</email>
<xsl:template match="curso"> </docente>
<alunos>
<html> <aluno>
<body> <nome>Ana</nome>
<html>
Resultado

<xsl:apply-templates select="*"/> <email>a@fe.up.pt</email>


<body>
</body> </aluno>
</html> <div>nome:Joo</div> <aluno>
</xsl:template> <div>email:j@fe.up.pt</div> <nome>Rita</nome>
</xsl:stylesheet> <div>nome:Ana</div> <email>r@fe.up.pt</email>
<div>email:a@fe.up.pt</div> </aluno></alunos></disciplina></curso
<div>nome:Rita</div>
<div>email:r@fe.up.pt</div>
</body>
</html>
08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 28
Regra mais especfica
 Em XSL bastante comum haver mais do que
uma regra que se aplica ao mesmo N
 Neste caso o Processador escolhe a Regra mais
especifica
 No entanto, h situaes onde difcil identificar
a regra mais especifica.
o Neste casos usa-
usa-se o atributo priority no elemento template
Pode assumir valores inteiros em 1 (menos prioritrio) e 20
(mais prioritrio)
Quando se omite o Processador classifica as regras com
prioridade entre 0 e 1 (ex: 0.9)

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 29
Regra mais especfica
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
ex9.xsl

<xsl:stylesheet version="1.0" <?xml-stylesheet type="text/xsl"


xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> href="ex8.xsl"?>
<xsl:template match="nome|email"> <curso nome="LCI">
<div> <disciplina nome="II" semestre="1">
<xsl:value-of select="name()"/>:<xsl:value-of select="."/> <docente><nome>Joo</nome>
<email>j@fe.up.pt</email>
</div>
</docente>
</xsl:template> <alunos><aluno>
<xsl:template match="aluno/nome"> <nome>Ana</nome>
<div style="color:red"> <email>a@fe.up.pt</email>
<xsl:value-of select="name()"/>:<xsl:value-of select="."/> </aluno>
</div> <aluno>
</xsl:template> <nome>Rita</nome>
<xsl:template match="curso"> <email>r@fe.up.pt</email>
<html><body> </aluno></alunos></disciplina>
Resultado

<html>
<div>nome:Joo</div> </curso>
<body>
<xsl:apply-templates select="*"/> <div>email:j@fe.up.pt</div>
</body> <div style="color:red">nome:Ana</div>
</html> <div>email:a@fe.up.pt</div>
</xsl:template> <div style="color:red">nome:Rita</div>
</xsl:stylesheet> <div>email:r@fe.up.pt</div>
08
08-
-11
11-
-10
</body></html>
http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 30
Incluir os Atributos
A regra omissa para os elementos no aplica
regras aos Atributos
 Quando usamos o apply-
apply-templates temos de
indicar se s queremos os N Elemento filho
(*) ou tambm os Ns Atributo filho (@*)
A regra omissa para os atributos obter o
seu valor

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 31
Incluir os Atributos
<?xml version="1.0" encoding="UTF-8"?>
ex10.xsl

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0" <?xml-stylesheet type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> href="ex8.xsl"?>
<xsl:template match="nome|email"> <curso nome="LCI">
<div> <disciplina nome="II" semestre="1">
<xsl:value-of select="name()"/>:<xsl:value-of select="."/> <docente>
</div> <nome>Joo</nome>
</xsl:template> <email>j@fe.up.pt</email>
<xsl:template match="aluno/nome"> </docente>
<alunos>
<div style="color:red"> <aluno>
<xsl:value-of select="name()"/>:<xsl:value-of select="."/> <nome>Ana</nome>
</div> <email>a@fe.up.pt</email>
<html>

Resultado
</xsl:template> </aluno>
<xsl:template match="curso"> <body>LCI <aluno>
<html> <div>nome:Joo</div>
<nome>Rita</nome>
<body> <div>email:j@fe.up.pt</div>
<email>r@fe.up.pt</email>
<xsl:apply-templates select="*|@*"/> </aluno></alunos></disciplina></curso
<div style="color:red">nome:Ana</div>
</body> <div>email:a@fe.up.pt</div>
</html> <div style="color:red">nome:Rita</div>
</xsl:template> <div>email:r@fe.up.pt</div>
</xsl:stylesheet> </body>
08
08-
-11
11-
-10 </html>
http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 32
Regras Omissas (verso final)
 Cada N Texto transformado no seu valor
 Cada N Atributo transformado no seu
valor
 Cada N Elemento Transformado no
resultado da transformao dos seus Ns
Elementos filho e dos seus Ns Texto filho

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 33
Regras omissas explicitadas
<?xml version="1.0" encoding="UTF-8"?>
Ex11.xsl

<xsl:stylesheet version="1.0" <?xml version="1.0" encoding="UTF-8"?>


<?xml-stylesheet type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> href="ex8.xsl"?>
<xsl:template match="text()"> <curso nome="LCI">
<xsl:value-of select="."/> <disciplina nome="II" semestre="1">
</xsl:template> <docente>
<xsl:template match="@*"> <nome>Joo</nome>
<xsl:value-of select="."/> <email>j@fe.up.pt</email>
</xsl:template> </docente>
<xsl:template match="*"> <alunos>
<aluno>
<xsl:apply-templates select="text()|*"/> <nome>Ana</nome>
</xsl:template> <email>a@fe.up.pt</email>
</xsl:stylesheet> </aluno>
<aluno>
<nome>Rita</nome>
Jooj@fe.up.ptAnaa@fe.up.ptRitar@fe.up.pt
Resultado

<email>r@fe.up.pt</email>
</aluno>
</alunos>
</disciplina>
</curso>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 34
Atributos com valores dinmicos
 Quando queremos gerar um elemento com um valor
dinmico usamos o elemento value
value--of,
of, EX:
<div>
div>
<xsl:value
xsl:value--of select=.>
select=.>
</div
</div>>

 Mas para obter o valor do atributo a seguinte


construo no seria XML bem formado
<div class=
class=<
<xsl:value
xsl:value--of select =.>

 A soluo encontrada foi o uso de chavetas para


obter o valor da expresso Xpath
<div class=
class={.}>

08
08-
-11
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 35
Atributos com valores dinmicos
<?xml version="1.0" encoding="UTF-8"?>
Ex11.xsl

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


<xsl:template match="*">
<div style="border:{count(ancestor::*)}px solid blue; margin: 2px">
<xsl:value-of select="name()"/> <?xml-stylesheet type="text/xsl" href="ex12.xsl"?>
<div style="margin-left:1em"> <curso nome="LCI" site="http://escola.pt">
<xsl:apply-templates select="node()|@*"/> <disciplina nome="II" semestre="1"
</div> site="http://escola.pt/disciplina.html">
<docente>
</div>
<nome>Joo</nome>
</xsl:template> <email>j@fe.up.pt</email>
<xsl:template match="@site"> </docente>
<a href="{.}" style="display:block"> <alunos>
<xsl:value-of select="."/> <aluno>
</a> <nome>Ana</nome>
</xsl:template> <email>a@fe.up.pt</email>
<xsl:template match="email/text()"> </aluno>
<aluno>
<a href="mailto:{.}">
<nome>Rita</nome>
<xsl:value-of select="."/> <email>r@fe.up.pt</email>
</a> </aluno></alunos></disciplina>
</xsl:template> </curso>
<xsl:template match="/"><html><body><xsl:apply-templates/></body>/html></xsl:template>
08
08-
-11</xsl:stylesheet>
11-
-10 http://www.fe.up.pt/~jvv/Assuntos/XSL/xsl.pdf 36
Desenvolvimento de
XSLTs
Sugesto de Abordagem do problema
Transformao de rvores
/ <Alunos>
<aluno>
<nome>Rute</nome>
Alunos <email>r@m.pt</email>
aluno aluno </aluno>
<aluno>
nome email nome email <nome>Lusa</nome>
<email>l@m.pr</email>
Rute r@m.pt Lusa l@m.pt </aluno>
</Alunos>
XSLT ?
<html>
/ <body>
<table>
html <tr>
<td>Rute</td>
body <td>r@m.pt</td>
table </tr>
<tr>
tr tr <td>Rute</td>
<td>r@m.pt</td>
td td td td </tr>
</table>
Rute r@m.pt Lusa l@m.pt </body>
</html>
Passo 1: mapear os Ns
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt
Passo 2: escrever as regras
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
Passo 2: escrever as regras
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match="nome|email">
<td>
<xsl:apply-templates select="node()"/>
</td>
</xsl:template>
Passo 2: escrever as regras
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match=aluno">
<tr>
<xsl:apply-templates select="node()"/>
</tr>
</xsl:template>
Passo 2: escrever as regras
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match="Alunos">
<html>
<body>
<table>
<xsl:apply-templates select="node()"/>
</table>
</body>
</html>
</xsl:template>
Resultado Final
<Alunos> <xsl:stylesheet version="1.0"
<aluno> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<nome>Rute</nome> <xsl:template match="Alunos">
<email>r@m.pt</email> <html>
</aluno> <body>
<aluno> <table>
<nome>Lusa</nome> <xsl:apply-templates select="node()"/>
<email>l@m.pt</email> </table>
</aluno> </body>
</Alunos> </html>
</xsl:template>
<xsl:template match=aluno">
<html> <tr>
<body> <xsl:apply-templates select="node()"/>
<table> </tr>
<tr> </xsl:template>
<td>Rute</td> <xsl:template match="nome|email">
<td>r@m.pt</td> <td>
</tr> <xsl:apply-templates select="node()"/>
<tr> </td>
<td>Rute</td> </xsl:template>
<td>r@m.pt</td> <xsl:template match="text()">
</tr> <xsl:value-of select="."/>
</table> </xsl:template>
</body> </xsl:stylesheet>
</html>
Mapeamentos
Alternativos
Um mapeamento alternativo
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match="nome|email">
<td>
<xsl:value-of select=text()"/>
</td>
</xsl:template>
Resultado Final
<Alunos> <xsl:stylesheet version="1.0"
<aluno> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<nome>Rute</nome> <xsl:template match="Alunos">
<email>r@m.pt</email> <html>
</aluno> <body>
<aluno> <table>
<nome>Lusa</nome> <xsl:apply-templates select="node()"/>
<email>l@m.pt</email> </table>
</aluno> </body>
</Alunos> </html>
</xsl:template>
<xsl:template match=aluno">
<html> <tr>
<body> <xsl:apply-templates select="node()"/>
<table> </tr>
<tr> </xsl:template>
<td>Rute</td> <xsl:template match="nome|email">
<td>r@m.pt</td> <td>
</tr> <xsl:value-of select=text()"/>
<tr> </td>
<td>Rute</td> </xsl:template>
<td>r@m.pt</td> </xsl:stylesheet>
</tr>
</table>
</body>
</html>
Outro mapeamento alternativo
/ /
Alunos html
aluno aluno body
nome email nome email table
Rute r@m.pt Lusa l@m.pt tr tr
td td td td
Rute r@m.pt Lusa l@m.pt

<xsl:template match=aluno">
<tr>
<td>
<xsl:value-of select=nome/text()"/>
</td>
<td>
<xsl:value-of select=email/text()"/>
</td>
</tr>
</xsl:template>
Resultado Final
<Alunos> <xsl:stylesheet version="1.0"
<aluno> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<nome>Rute</nome> <xsl:template match="Alunos">
<email>r@m.pt</email> <html>
</aluno> <body>
<aluno> <table>
<nome>Lusa</nome> <xsl:apply-templates select="node()"/>
<email>l@m.pt</email> </table>
</aluno> </body>
</Alunos> </html>
</xsl:template>
<xsl:template match=aluno">
<html> <tr>
<body> <td>
<table> <xsl:value-of select=nome/text()"/>
<tr> </td>
<td>Rute</td> <td>
<td>r@m.pt</td> <xsl:value-of select=email/text()"/>
</tr> </td>
<tr> </tr>
<td>Rute</td> </xsl:template>
<td>r@m.pt</td> </xsl:stylesheet>
</tr>
</table>
</body>
</html>
Mais elementos e
funes do XSLT
Copiar Ns um a um
<?xml version="1.0"?> <?xml version="1.0"?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <?xml-stylesheet type="text/xsl"


href="ex1.xsl ?> href="ex1.xsl ?>
<R> <R>
<a> <a>
<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/><!--
<!-- comentrio1 --> comentrio1 -->
</a> </a>
<a> <a>
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </a><!-- ltimo comentrio-->
<!-- ltimo comentrio--> </R>
</R>

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> O elemento
<xsl:template match="node()|@*">
<xsl:copy>
xsl:copy copia o N
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Copiar sub-rvore
<?xml version="1.0"?> <?xml version="1.0?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <?xml-stylesheet type="text/xsl"


href="ex1.xsl ?> href="ex1.xsl"?>
<R> <R> Esta sub-
<a> <a> rvore foi
<b x="0" y="1"/> <b x="0"/>
<b x="2" y="3"/> <b x="2"/><!--
copiada
<!-- comentrio1 --> comentrio1 --> integralmente
</a> </a>
<a> <a>
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </a><!-- ltimo comentrio-->
<!-- ltimo comentrio--> </R>
</R>

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> O elemento xsl:copy-of copia a sub-
<xsl:template match="@y"/> rvore indicada no atributo select
<xsl:template match="a[2]">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Inst. de processamento
<?xml version="1.0"?> <?xml version="1.0"?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <R>


href="ex1.xsl ?> <a>
<R> <b x="0" y="1"/>
<a> <b x="2" y="3"/><!--
<b x="0" y="1"/> comentrio1 -->
<b x="2" y="3"/> </a>
<!-- comentrio1 --> <a>
</a> <c x="0" y="1"/>
<a> <c x="2" y="3"/>
<c x="0" y="1"/> </a><!-- ltimo comentrio-->
<c x="2" y="3"/> </R>
</a>
<!-- ltimo comentrio-->
</R>

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*"> A funo processing-
<xsl:copy> instruction() devolve
<xsl:apply-templates select="node()|@*"/> os Ns Instruo de
</xsl:copy> Processamento
</xsl:template>
<xsl:template match="processing-instruction()"/>
</xsl:stylesheet>
Comentrios
<?xml version="1.0"?> <?xml version="1.0"?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <R>


href="ex1.xsl ?> <a>
<R> <b x="0" y="1"/>
<a> <b x="2" y="3"/>
<b x="0" y="1"/> </a>
<b x="2" y="3"/> <a>
<!-- comentrio1 --> <c x="0" y="1"/>
</a> <c x="2" y="3"/>
<a> </a>
<c x="0" y="1"/> </R>
<c x="2" y="3"/>
</a>
<!-- ltimo comentrio-->
</R>

<?xml version="1.0" encoding="UTF-8"?>


A funo
Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> comment()
<xsl:template match="node()|@*"> devolve os Ns
<xsl:copy> comentrio
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction()|comment()"/>
</xsl:stylesheet>
Gerao de comentrios

Comentrio gerado pelo XSLT


<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <R>


href="ex1.xsl ?> <a>
<R> <b x="0" y="1"><!--P=2--></b>
<a> <b x="2" y="3"><!--P=4--></b>
<b x="0" y="1"/> <!--P=2--></a>
<b x="2" y="3"/> <a>
<!-- comentrio1 --> <c x="0" y="1"><!--P=2--></c>
</a> <c x="2" y="3"><!--P=4--></c>
<a> <!--P=4--></a>
<c x="0" y="1"/> <!--P=2--></R>
<c x="2" y="3"/>
Estes valores esto correctos porque
</a> h um N de texto entre cada elemento
<!-- ltimo comentrio--> devido ao newline da mudana de linha
</R>

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0" O elemento xsl:comment


xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> gera um N comentrio
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:comment>P=<xsl:value-of select="position()"/></xsl:comment>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction()|comment()"/>
</xsl:stylesheet>
Gerao de I. de Proc.
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>

Result.xml
Ex1.xml

<?xml-stylesheet type="text/xsl" <?xml-stylesheet href="style.css"


href="ex1.xsl ?> type="text/css"?>
<R> <R>
<a> <a>
<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
<!-- comentrio1 --> </a>
</a> <a>
<a> <c x="0" y="1"/>
<c x="0" y="1"/> <c x="2" y="3"/>
<c x="2" y="3"/> </a>
</a> </R> Instruo de Processamento
<!-- ltimo comentrio--> gerado pelo XSLT
</R>
<?xml version="1.0" encoding="UTF-8"?>
Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy> O elemento xsl:processing-instruction
</xsl:template>
<xsl:template match="comment()"/> gera um N Instruo de Processamento
<xsl:template match="/">
<xsl:processing-instruction name="xml-stylesheet">href="style.css"
type="text/css"</xsl:processing-instruction>
<xsl:apply-templates select="*"/>
</xsl:template></xsl:stylesheet>
Gerao de Elementos
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>

Result.xml
Ex1.xml

<R> <R>
<a> <a>
<b x="0" y="1"/> <b><x>0</x><y>1</y></b>
<b x="2" y="3"/> <b><x>2</x><y>3</y></b>
</a> </a>
<a> <a>
<c x="0" y="1"/> <c><x>0</x><y>1</y></c>
<c x="2" y="3"/> <c><x>2</x><y>3</y></c>
</a> </a>
</R> </R>

Elementos
gerados pelo XSLT

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/> O elemento xsl:element gera
</xsl:element> um N Elemento cujo nome
</xsl:template> indicado no atributo name
</xsl:stylesheet>
Gerao de Atributos
<?xml version="1.0?> <?xml version="1.0"?>

Result.xml
Ex1.xml

<R> <R>
<a> <a>
<b><x>0</x><y>1</y></b> <b x="0" y="1"/>
<b><x>2</x><y>3</y></b> <b x="2" y="3"/>
</a> </a>
<a> <a>
<c><x>0</x><y>1</y></c> <c x="0" y="1"/>
<c><x>2</x><y>3</y></c> <c x="2" y="3"/>
</a> </a>
</R> </R>

Atributos gerados
pelo XSLT

<?xml version="1.0" encoding="UTF-8"?>


Ex1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/> O elemento xsl:attribute gera
</xsl:attribute> um N Atributo cujo nome
</xsl:template> indicado no atributo name
</xsl:stylesheet>
Gerao de IDs
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-

Result.xml
Ex1.xml

<R> 8"?><R ID="d0e1">


<a> <a ID="d0e3">
<b x="0" y="1"/> <b ID="d0e5" x="0" y="1"/>
<b x="2" y="3"/> <b ID="d0e7" x="2" y="3"/>
</a> </a>
<a> <a ID="d0e10">
<c x="0" y="1"/> <c ID="d0e12" x="0" y="1"/>
<c x="2" y="3"/> <c ID="d0e14" x="2" y="3"/>
</a> </a> Valor nico
</R> </R>

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
Ex1.xsl

<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> A funo generate-id gera
<xsl:template match="*"> um valor nico dentro do
<xsl:copy> documento resultante
<xsl:attribute name="ID">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Espaos de Nomes: problema
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <a>
<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </a>
<a> <a>
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </a>
</R> Espao de nomes </R>
por omisso

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
Ex1.xsl

<xsl:apply-templates select="node()|@*"/>
</xsl:copy> No surge no
</xsl:template>
<xsl:template match="b"> resultado!
<bbb> Porque?
<xsl:apply-templates select="node()|@*"/>
</bbb>
</xsl:template> O elemento b do espao de nomes
</xsl:stylesheet> annimo e no documento XML fonte
os elementos so todos do espao
de nomes urn:feup-isi
Indicao do Espao de Nomes
<R <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

xmlns="urn:feup-isi"> <a>
<a> <bbb xmlns="" xmlns:fe="urn:feup-isi" x="0" y="1"/>
<b x="0" y="1"/> <bbb xmlns="" xmlns:fe="urn:feup-isi" x="2" y="3"/>
<b x="2" y="3"/> </a>
</a> <a>
<a> <c x="0" y="1"/>
<c x="0" y="1"/> <c x="2" y="3"/>
<c x="2" y="3"/> </a>
</a> </R>
</R>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
Ex1.xsl

<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fe:b">
<bbb>
<xsl:apply-templates select="node()|@*"/>
</bbb>
</xsl:template>
</xsl:stylesheet>
Espao de Nomes annimo
<R <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

xmlns="urn:feup-isi"> <a>
<a> <bbb xmlns="" xmlns:fe="urn:feup-isi" x="0" y="1"/>
<b x="0" y="1"/> <bbb xmlns="" xmlns:fe="urn:feup-isi" x="2" y="3"/>
<b x="2" y="3"/> </a>
</a> <a>
<a> <c x="0" y="1"/>
<c x="0" y="1"/> <c x="2" y="3"/>
<c x="2" y="3"/> </a>
</a> </R>
</R>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
Ex1.xsl

<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> O elemento bbb criado no
<xsl:template match="fe:b"> espao de nomes annimo
<bbb>
<xsl:apply-templates select="node()|@*"/>
</bbb>
</xsl:template>
</xsl:stylesheet>
Criao de Elementos no Espao de Nomes annimo
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <aaa xmlns="" xmlns:fe="urn:feup-isi">


<b x="0" y="1"/> <b xmlns="urn:feup-isi" x="0" y="1"/>
<b x="2" y="3"/> <b xmlns="urn:feup-isi" x="2" y="3"/>
</a> </aaa>
<a> <aaa xmlns="" xmlns:fe="urn:feup-isi">
<c x="0" y="1"/> <c xmlns="urn:feup-isi" x="0" y="1"/>
<c x="2" y="3"/> <c xmlns="urn:feup-isi" x="2" y="3"/>
</a> </aaa>
</R> </R>

Esta declarao, gerada pelo processador de XSLT, Os elementos b e c so


necessria, caso contrrio o elemento c pertenceria ao espao
de nomes annimo, devido associao, no elemento aaa, do do espao de nomes
prefixo omisso a esse espao de nomes (xmlns=") urn:feup-isi
<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> O elemento aaa criado no
<xsl:template match="fe:a"> espao de nomes annimo
<aaa>
<xsl:apply-templates select="node()|@*"/>
</aaa>
</xsl:template>
</xsl:stylesheet>
Criao de Elementos no Espao de Nomes do documento
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <fe:aaa xmlns:fe="urn:feup-isi">


<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </fe:aaa>
<a> <fe:aaa xmlns:fe="urn:feup-isi">
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </fe:aaa>
</R> </R>

<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> O elemento aaa criado no espao
<xsl:template match="fe:a"> de nomes do documento
<fe:aaa>
<xsl:apply-templates select="node()|@*"/>
</fe:aaa>
</xsl:template>
</xsl:stylesheet>
Criao de Elementos no Espao de Nomes do documento
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <aaa xmlns:fe="urn:feup-isi">


<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </aaa>
<a> <aaa xmlns:fe="urn:feup-isi">
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </aaa>
</R> </R>

Esta declarao do espao de nomes


desnecessria mas a sua ocorrncia
depende do processador de XSLT

<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template> O elemento aaa criado no espao
<xsl:template match="fe:a"> de nomes do documento
<aaa xmlns="urn:feup-isi">
<xsl:apply-templates select="node()|@*"/>
</aaa>
</xsl:template>
</xsl:stylesheet>
Criao de Elementos no Espao de Nomes do documento
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <fe:aaa xmlns:fe="urn:feup-isi">


<b x="0" y="1"/> <fe:b x="0" y="1"/>
<b x="2" y="3"/> <fe:b x="2" y="3"/>
</a> </fe:aaa>
<a> <fe:aaa xmlns:fe="urn:feup-isi">
<c x="0" y="1"/> <fe:c x="0" y="1"/>
<c x="2" y="3"/> <fe:c x="2" y="3"/>
</a> </fe:aaa>
</R> </R>

Resultado produzido pelo saxon usando o mesmo XSLT do slide anterior.


O resultado o mesmo o que varia a apresentao.
Os elementos pertencem todos ao mesmo espao de nomes, embora
uns tenham prefixo (R//*) e outros no (R)
<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fe:a">
<aaa xmlns="urn:feup-isi">
<xsl:apply-templates select="node()|@*"/>
</aaa>
</xsl:template>
</xsl:stylesheet>
O xsl:element e o Espao de Nomes
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <aaa>
<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </aaa>
<a> <aaa>
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </aaa>
</R> </R>

<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy> O espao de nomes do elemento
</xsl:template>
<xsl:template match="fe:a"> indicado no atributo namespace
<xsl:element name="aaa" namespace="urn:feup-isi">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
O xsl:element e o Espao de Nomes
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <pr:aaa xmlns:pr="urn:feup-isi">


<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </pr:aaa>
<a> <pr:aaa xmlns:pr="urn:feup-isi">
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </pr:aaa>
</R> </R>

<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fe="urn:feup-isi">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fe:a">
<xsl:element name="pr:aaa" namespace="urn:feup-isi">
<xsl:apply-templates select="node()|@*"/>
</xsl:element> Este prefixo automaticamente associado aos
</xsl:template>
</xsl:stylesheet>
espao de nomes indicado no atributo namespace
Qualquer Espao de Nomes
<R xmlns="urn:feup-isi"> <R xmlns="urn:feup-isi">

Result.xml
Ex1.xml

<a> <aaa>
<b x="0" y="1"/> <b x="0" y="1"/>
<b x="2" y="3"/> <b x="2" y="3"/>
</a> </aaa>
<a xmlns=""> <aaa xmlns="">
<c x="0" y="1"/> <c x="0" y="1"/>
<c x="2" y="3"/> <c x="2" y="3"/>
</a> </aaa>
</R> </R>

Este template substitui qualquer elemento a, de qualquer


namespace, por um elemento aaa do mesmo namespace
<xsl:stylesheet version="1.0"
Ex1.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy> A funo local-name
<xsl:apply-templates select="node()|@*"/> devolve o nome do
</xsl:copy> N sem o prefixo
</xsl:template>
<xsl:template match="*[local-name()='a']">
<xsl:element name="aaa" namespace="{namespace-uri()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
A funo namespace-uri devolve
</xsl:stylesheet> o espao de nomes do N

You might also like