这短变换产生的通缉的结果,即使在body
的col
元素不按照正确的顺序:
-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vColNames">
<xsl:text>|</xsl:text>
<xsl:for-each select="/*/*/header/col">
<xsl:value-of select="concat(@name, '|')"/>
</xsl:for-each>
</xsl:variable>
<xsl:template match="table">
<table border="1">
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="header">
<thead>
<tr>
<xsl:apply-templates/>
</tr>
</thead>
</xsl:template>
<xsl:template match="header/col">
<td><xsl:value-of select="@name"/></td>
</xsl:template>
<xsl:template match="body/row">
<tr>
<xsl:apply-templates select=
"col[contains($vColNames,concat('|',@name, '|'))]">
<xsl:sort select=
"string-length(substring-before($vColNames, @name))"/>
</xsl:apply-templates>
</tr>
</xsl:template>
<xsl:template match="col">
<td><xsl:value-of select="."/></td>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
-
当应用在这个文件上(基本上是提供的XML文档,但是中的元素在body
是混合顺序):
-
<mylist name="test">
<title>--$title$- not found</title>
<table>
<header>
<col name="firstname" title="--$firstname$- not found"/>
<col name="lastname" title="--$lastname$- not found"/>
<col name="country" title="--$country$- not found"/>
</header>
<body>
<row>
<col name="lastname">Smith</col>
<col name="firstname">John</col>
<col name="country">ENGLAND</col>
</row>
<row>
<col name="country">USA</col>
<col name="firstname">Peter</col>
<col name="lastname">Scott</col>
</row>
</body>
</table>
</mylist>
想要的,正确的结果产生:
-
<table border="1">
<thead>
<tr>
<td>firstname</td>
<td>lastname</td>
<td>country</td>
</tr>
</thead>
<tr>
<td>John</td>
<td>Smith</td>
<td>ENGLAND</td>
</tr>
<tr>
<td>Peter</td>
<td>Scott</td>
<td>USA</td>
</tr>
</table>
这是比较容易,如果提供一个答案您将张贴HTML,而不是呈现的HTML的图像。 –
好问题,+1。查看我的答案,获得更强大的解决方案,它不仅更短,而且即使在'body'中的col'元素混合排列的情况下也能产生正确的结果。 –