我目前有一个这样的XML文档:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="./transform.xsl"?>
<Documentation><Tables>
<TABLE TABLE_NAME="FirstTable">
<COLUMN COLUMN_NAME="FirstColumn" ORDINAL_POSITION="1" IS_NULLABLE="NO" DATA_TYPE="int" />
<COLUMN COLUMN_NAME="SecondColumn" ORDINAL_POSITION="2" IS_NULLABLE="NO" DATA_TYPE="int" />
</TABLE>
...
</Tables><Comments>
<COMMENT ForTable="FirstTable" ForColumn="FirstColumn">Description of FirstColumn</COMMENT>
</Comments></Documentation>我的问题是,在对表和列进行循环时,如何获得注释的值?我有:
<xsl:for-each select="//TABLE">
...
<xsl:for-each select="COLUMN">
...
<xsl:value-of select="//COMMENT[@ForTable = @TABLE_NAME and @ForColumn=@COLUMN_NAME]" />但这不管用。有什么建议吗?
发布于 2009-12-16 18:24:14
解决这个问题的另一个变种是:
<xsl:for-each select="/Documentation/Tables/TABLE">
<!-- select all comments for the current table name… -->
<xsl:variable name="$comments" select="
/Documentation/Comments/COMMENT[@ForTable = current()/@TABLE_NAME]
"/>
<xsl:for-each select="COLUMN">
<!-- …of those, select the one with the current column name -->
<xsl:value-of select="
$comments[@ForColumn = current()/@COLUMN_NAME]
" />
</xsl:for-each>
</xsl:for-each>好处是:
-
-可以更快地运行,因为它查看的是较小的、预先过滤的数据集
请注意:
在predicates.
//,我使用current()来引用XPath中的XSLT上下文节点,因为它具有非常糟糕的性能特征,如果可能的话,就不应该使用它。您的输入文档结构意味着//不是必需的。发布于 2009-12-16 14:54:43
它不起作用,因为value - of的select语句是“获取其ForTable属性与其表名属性具有相同值且其ForColumn属性与其列名称属性具有相同值的所有注释的值”。
试试像这样的东西
<xsl:for-each select="//TABLE">
<xsl:variable name="table_name" select="@TABLE_NAME"/>
...
<xsl:for-each select="COLUMN">
<xsl:variable name="column_name" select="@COLUMN_NAME"/>
...
<xsl:value-of select="//COMMENT[@ForTable=$table_name and @ForColumn=$column_name]" />https://stackoverflow.com/questions/1912697
复制相似问题