【发布时间】:2010-05-27 18:30:57
【问题描述】:
给定以下代码:
<database>
<table name="table1">
<column name="id"/>
<column name="created_at"/>
</table>
<table name="table2">
<column name="id"/>
<column name="updated_at"/>
</table>
</database>
如果特定表的名称有特定列,我希望能够使用 xsl:key 进行测试。例如,我想知道一个表是否有一个“created_at”列,这样我就可以在转换中编写特定的代码。
我得到了一个通用键,可以测试 any 表是否具有按名称给定的列,但还没有弄清楚如何使其特定于转换当前正在使用的表。
<xsl:key name="columnTest" match="column" use="@name"/>
<xsl:for-each select="database/table">
<xsl:choose>
<xsl:when test="key('columnTest', 'created_at')">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>false</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
所以我最终为所有表格都设置了“真”。任何指导将不胜感激。
【问题讨论】:
-
好问题 (+1)。请参阅我的答案以获得完整的解决方案。