2010-08-09 31 views
5

tập tin XML của tôi là như sau:Gán CSS class cho phần tử trong một 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> 

trong xsl của tôi, trong khi hiển thị nội dung của một hàng cụ thể, tôi muốn thêm một lớp học để các yếu tố html mà sẽ chỉ định loại hàng của nó. Tôi đã thử sử dụng xsl: chọn và gán giá trị cho biến xsl: nhưng điều đó không hoạt động. Tôi đang cố gắng để hiển thị các hàng như

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

đoạn tập tin 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

Bạn đã đăng XML và kết quả mong muốn của mình. Bạn có thể đăng XSL bạn đang gặp sự cố không? – Oded

+0

Câu hỏi hay (+1). Xem câu trả lời của tôi cho một giải pháp hoàn chỉnh đơn giản và tránh việc viết mã không cần thiết về logic quyết định. –

Trả lời

9

Bạn cần phải thêm nó như là một thuộc tính đến các yếu tố:

<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

Giải pháp ngây thơ nhất sẽ sử dụng hướng dẫn xsl:choose như sau:

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

cách khác là nên có một bản đồ inline (hoặc thông qua fn:document()) như:

<li class="{$map[@type = current()/type]|$map[not(@type)]}"/> 

Với điều này là yếu tố cấp cao nhất

<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 từ tôi cho một giải pháp thực sự tốt. –

4

Nó không phải là cần thiết ở tất cả để sử dụng <xsl:choose> trong chuyển đổi:

Chuyển đổi này:

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

khi áp dụng trên tài liệu XML cung cấp:

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

tạo ra truy nã, chính xác kết quả:

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

Do lưu ý:

  1. Chỉ các mẫu đơn giản và <xsl:apply-templates> mới được sử dụng. Do đó, cơ hội thực hiện một lỗi được giảm thiểu.

  2. Mã này vốn đã mở rộng và có thể duy trì. Nếu một loại hàng mới được giới thiệu thì không cần thay đổi mẫu nào - chỉ cần thêm mẫu mới và đơn giản mới khớp với phần tử row có con số rowType với giá trị mới.

  3. Các ánh xạ giữa rowType và giá trị tương ứng của thuộc tính class thể được chỉ định trong một đặc biệt toàn cầu cấp phần tử và/hoặc biến namespaced (as done trong dung dịch Alejandro của), hoặc thậm chí trong một tài liệu XML riêng biệt. Sau đó, chúng tôi có thể chỉ có một mẫu duy nhất, phù hợp với row.

Các vấn đề liên quan