XPath - curinga
XPath define os seguintes curingas em nós a serem usados com as expressões XPath.
S.No. | WildCard e descrição |
---|---|
1 | * usado para corresponder a qualquer nó. |
2 | . usado para corresponder ao nó atual no contexto. |
3 | @* usado para corresponder a qualquer atributo |
4 | node() usado para combinar nó de qualquer tipo |
Exemplo
Este exemplo cria uma tabela do elemento <student> com seus detalhes, iterando sobre cada aluno.
students.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
<student rollno = "393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Students</h2>
<xsl:apply-templates select = "class/*" />
</body>
</html>
</xsl:template>
<xsl:template match = "class/*">
<xsl:apply-templates select = "@rollno" />
<xsl:apply-templates select = "firstname" />
<xsl:apply-templates select = "lastname" />
<xsl:apply-templates select = "nickname" />
<xsl:apply-templates select = "marks" />
<br />
</xsl:template>
<xsl:template match = "@rollno">
<span style = "font-size = 22px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "firstname">
First Name:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "lastname">
Last Name:<span style = "color:green;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "nickname">
Nick Name:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "marks">
Marks:<span style = "color:gray;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>