Portál AbcLinuxu, 6. listopadu 2025 13:44
Řešení dotazu:
<?xml version="1.0"?> <data> <a> <item>Alpha</item> <item>Bravo</item> <item>Charlie</item> </a> <b> <item>Zulu</item> </b> </data>Pak následující dorovná buňky pomlčkama.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<head><title>Title</title></head>
<body>
<xsl:apply-templates select="data" />
</body>
</html>
</xsl:template>
<xsl:template match="data">
<xsl:variable name="THIS" select="." />
<xsl:variable name="BOTH" select="a/item | b/item" />
<table border="1">
<xsl:for-each select="$BOTH[(position() <= count($THIS/a/item)) or (position() <= count($THIS/b/item))]">
<xsl:variable name="INDEX" select="position()" />
<tr>
<td>
<xsl:value-of select="$INDEX" />
</td>
<td>
<xsl:choose>
<xsl:when test="$INDEX <= count($THIS/a/item)">
<xsl:apply-templates select="$THIS/a/item[$INDEX]" />
</xsl:when>
<xsl:otherwise>
-
</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="$INDEX <= count($THIS/b/item)">
<xsl:apply-templates select="$THIS/b/item[$INDEX]" />
</xsl:when>
<xsl:otherwise>
-
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="item">
<b><xsl:value-of select="." /></b>
</xsl:template>
</xsl:stylesheet>
Trik je v tom, že se vyberou oba dva uzly pro for-each iteraci a v každém průchodu se kontroluje, jestli tam takový prvek je.
Ale nevím, jak to bude rychlé pro větší data.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.