簡體   English   中英

篩選xsl:value-of屬性

[英]Filtering xsl:value-of on attributes

我目前有一個像這樣的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>

我的問題是,在遍歷表和列時如何獲取COMMENT的值? 我有:

<xsl:for-each select="//TABLE">
  ...
  <xsl:for-each select="COLUMN">
    ...
    <xsl:value-of select="//COMMENT[@ForTable = @TABLE_NAME and @ForColumn=@COLUMN_NAME]" />

但這是行不通的。 有什么建議嗎?

這是行不通的,因為value of的選擇是說“獲取其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]" />

解決此問題的另一種方法是:

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

好處是:

  • 您只需要一個變量,而不是兩個
  • 內部for-each可以運行得更快,因為它查看的是較小的預過濾數據集

注意:

  • 我使用current()來引用XPath謂詞中的XSLT上下文節點。
  • 我避免在XPath表達式中使用// ,因為它的性能特性確實很差,如果可能的話,不應使用。 您的輸入文檔結構意味着//是不必要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM