Como Acceder a Valores en XSL anidados.
Nested XSL
<mainItem>
<item selected="2">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
<item selected="2">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
<item selected="3">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
</mainItem>
<?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>Selected</th>
<th>text opt</th>
</tr>
<xsl:for-each select="mainItem/item">
<xsl:variable name="selectedItem" select="@selected"/>
<tr>
<td><xsl:value-of select="@selected" /></td>
<td>
<xsl:for-each select="options/opt">
<xsl:if test="$selectedItem=@id">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
resultado:
<mainItem>
<item selected="2">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
<item selected="2">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
<item selected="3">
<options>
<opt id="1">uno</opt>
<opt id="2">dos</opt>
<opt id="3">tres</opt>
</options>
</item>
</mainItem>
<?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>Selected</th>
<th>text opt</th>
</tr>
<xsl:for-each select="mainItem/item">
<xsl:variable name="selectedItem" select="@selected"/>
<tr>
<td><xsl:value-of select="@selected" /></td>
<td>
<xsl:for-each select="options/opt">
<xsl:if test="$selectedItem=@id">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
resultado:
| Selected | text opt |
|---|---|
| 2 | dos |
| 2 | dos |
| 3 | tres |
Comentarios
Publicar un comentario