Latest web development tutorials

XSLT <xsl: for-each> elemento

<Xsl: for-each> elemento consente di loop in XSLT.


<Xsl: for-each> elemento

XSL <xsl: for-each> elemento può essere usato per selezionare ogni elemento XML nodo specificato set:

Esempi

<?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>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Prova »

Nota: Il valore dell'attributo selectè un'espressione XPath. Questo funziona in modo simile all'espressione XPath individuare un file di sistema, in cui la barra (/) per selezionare una sottodirectory.


uscita del filtro

Con <xsl: for-each> elemento per aggiungere un attributo di selezione discriminante, possiamo anche filtrare i risultati di un output file XML.

<Xsl: for-each select = "catalogo / cd [artista = 'Bob Dylan']">

Filtro operatori legittimi:

  • = (Uguale)
  • ! = (Non uguale)
  • & Lt; (meno di)
  • & Gt; (maggiore di)

Vedere il foglio di stile XSL regolata:

Esempi

<?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>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Prova »