2010-08-09 61 views
5

我的XML文件如下:分配CSS類元素在XSL

<worksheet> 
<row> 
<rowTitle>RT1</rowTitle> 
<rowType>yesorno</rowType> 
<subLine>subLine1Content</subLine> 
<subLine>subLine2Content</subLine> 
</row> 
<row> 
<rowTitle>RT2</rowTitle> 
<rowType>operation</rowType> 
<subLine>subLine1Content</subLine> 
<subLine>subLine2Content</subLine> 
<subLine>subLine3Content</subLine> 
</row> 
. 
. 
</worksheet> 
在我的XSL

,同時顯示特定行的內容,我想一個類添加到HTML元素將指定它所在的行的類型。我嘗試使用xsl:選擇並將值分配給xsl:變量,但這不起作用。 我想顯示行作爲

<ol> 
<li class="rowTypeBoolean"> 
RT1 
<ul><li>subLineContent1</li> 
<li>subLineContent2</li></ul> 
</li> 
<li class="rowTypeOptions"> 
RT2 
<ul><li>subLineContent1</li> 
<li>subLineContent2</li> 
<li>subLineContent3</li></ul> 
</li> 
.  
. 
</ol> 

XSL文件片段

<xsl:template match="row"> 
     <li class="rowClass ${className}"> 
      <xsl:choose> 
       <xsl:when test="type = 'YESORNO'"> 
        <xsl:variable name="className" select="rowTypeBoolean"/> 
       </xsl:when> 
       <xsl:when test="type = 'OPTIONS'"> 
        <xsl:variable name="className" select="rowTypeOptions"/> 
       </xsl:when> 
       <xsl:when test="type = 'OPERATION'"> 
        <xsl:variable name="className" select="rowTypeOperation"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:variable name="className" select="rowTypeOther"/> 
       </xsl:otherwise> 
      </xsl:choose> 
      <span class="rowTitleClass"> 
       <xsl:value-of select="rowtitle"/> 
      </span> 
      <br/> 
      <ul class="subLineListClass"> 
       <xsl:apply-templates select="subLine"/> 
      </ul> 
     </li> 
</xsl:template> 
+0

你已經發布了你的XML和你想要的輸出。你可以發佈你目前遇到問題的XSL嗎? – Oded 2010-08-09 19:05:01

+0

好問題(+1)。查看我的答案,獲得一個簡單的完整解決方案,避免對決策邏輯進行不必要的編碼。 – 2010-08-09 20:17:54

回答

9

你需要把它作爲一個屬性添加到元素:

<li> 
     <xsl:choose> 
      <xsl:when test="type = 'YESORNO'"> 
       <xsl:attribute name="className">rowTypeBoolean</xsl:attribute> 
      </xsl:when> 
      <xsl:when test="type = 'OPTIONS'"> 
       <xsl:attribute name="className">rowTypeOptions</xsl:attribute> 
      </xsl:when> 
      <xsl:when test="type = 'OPERATION'"> 
       <xsl:attribute name="className">rowTypeOperation"</xsl:attribute> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:attribute name="className">rowTypeOther"</xsl:attribute> 
      </xsl:otherwise> 
     </xsl:choose> 
    </li> 
6

最天真的解決方案會使用xsl:choose這樣的指令:

<li> 
    <xsl:attribute name="className"> 
     <xsl:choose> 
      <xsl:when test="type = 'YESORNO'">rowTypeBoolean</xsl:when> 
      <xsl:when test="type = 'OPTIONS'">rowTypeOptions</xsl:when> 
      <xsl:when test="type = 'OPERATION'">rowTypeOperation</xsl:when> 
      <xsl:otherwise>rowTypeOther</xsl:otherwise> 
     </xsl:choose> 
    </xsl:attribute> 
</li> 

其他方式將是有一個在線地圖(或通過fn:document()),如:

​​

以此爲頂級元素

<map:map xmlns:map="map"> 
    <item type="YESORNO">rowTypeBoolean</item> 
    <item type="OPTIONS">rowTypeOption</item> 
    <item type="OPERATIONS">rowTypeOperation</item> 
    <item>rowTypeOther</item> 
</map:map> 

<xsl:variable name="map" select="document('')/*/map:map/*" xmlns:map="map"/> 
+2

+1對我來說是一個很好的解決方案。 – 2010-08-10 02:12:25

4

沒有必要在所有使用<xsl:choose>在轉化

此轉型:

<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:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="worksheet"> 
    <ol> 
    <xsl:apply-templates/> 
    </ol> 
</xsl:template> 

<xsl:template match="row[rowType='yesorno']"> 
    <li class="rowTypeBoolean"> 
    <xsl:apply-templates/> 
    </li> 
</xsl:template> 

<xsl:template match="row[rowType='operation']"> 
    <li class="rowTypeOperation"> 
    <xsl:apply-templates/> 
    </li> 
</xsl:template> 

<xsl:template match="row[rowType='options']"> 
    <li class="rowTypeOptions"> 
    <xsl:apply-templates/> 
    </li> 
</xsl:template> 

<xsl:template match="row"> 
    <li class="rowTypeOther"> 
    <xsl:apply-templates/> 
    </li> 
</xsl:template> 

<xsl:template match="subLine[1]"> 
    <ul> 
    <xsl:apply-templates select="../subLine" mode="process"/> 
    </ul> 
</xsl:template> 

    <xsl:template match="subLine" mode="process"> 
    <li><xsl:apply-templates/></li> 
    </xsl:template> 

</xsl:stylesheet> 

時所提供的XML文檔應用:

<worksheet> 
    <row> 
     <rowTitle>RT1</rowTitle> 
     <rowType>yesorno</rowType> 
     <subLine>subLine1Content</subLine> 
     <subLine>subLine2Content</subLine> 
    </row> 
    <row> 
     <rowTitle>RT2</rowTitle> 
     <rowType>operation</rowType> 
     <subLine>subLine1Content</subLine> 
     <subLine>subLine2Content</subLine> 
     <subLine>subLine3Content</subLine> 
    </row> 
</worksheet> 

產生想要的,正確的結果

<ol> 
    <li class="rowTypeBoolean"> 
     <rowTitle>RT1</rowTitle> 
     <rowType>yesorno</rowType> 
     <ul> 
      <li>subLine1Content</li> 
      <li>subLine2Content</li> 
     </ul> 
     <subLine>subLine2Content</subLine> 
    </li> 
    <li class="rowTypeOperation"> 
     <rowTitle>RT2</rowTitle> 
     <rowType>operation</rowType> 
     <ul> 
      <li>subLine1Content</li> 
      <li>subLine2Content</li> 
      <li>subLine3Content</li> 
     </ul> 
     <subLine>subLine2Content</subLine> 
     <subLine>subLine3Content</subLine> 
    </li> 
</ol> 

請注意

  1. 只有簡單的模板和<xsl:apply-templates>被使用。因此,犯錯的機會被最小化。

  2. 此代碼具有固有的可擴展性和可維護性。如果引入了新的行類型,則不需要更改任何現有模板 - 只需添加一個新的簡短模板即可將具有rowType子元素的row元素與新值相匹配。

  3. rowTypeclass屬性的相應值之間的映射可以在一個特殊的全局級別的命名空間元素和/或變量來指定(如在亞歷的溶液中進行),或者甚至在單獨的XML文檔。然後我們可以只有一個模板,匹配row