2009-12-16 27 views
0

我現在有這樣一個XML文檔:過濾的xsl:value-of的有關屬性

<?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 @[email protected]_NAME]" /> 

但這不起作用。有什麼建議?

回答

1

另一方案來解決,這將是:

<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> 

的好處是:

  • 你只需要一個變量,而不是兩個
  • 內部爲每個可以運行得更快,因爲它正在看小光伏,預過濾的數據集

注意的是:

  • 我使用current()來指代XSLT上下文節點的XPath謂詞內。
  • 我在XPath表達式中避免使用//,因爲它具有非常差的性能特性,如果可能的話不應使用。您的輸入文檔結構意味着//不是必需的。
+0

謝謝,我結束了使用的解決方案,而變量: 的 Todd 2009-12-16 22:53:43

+0

請注意,這可能效率較低,但對於小型輸入文檔而言,它是學術性的。 – Tomalak 2009-12-17 00:06:21

2

它不起作用,因爲select-value的值是「獲取ForTable屬性與TABLE NAME屬性具有相同值並且其ForColumn屬性與它們的COLUMN NAME屬性值相同的所有註釋的值」 。

嘗試像

<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]" />