将CSS类分配给XSL中的元素

cha*_*tta 7 xslt

我的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>
Run Code Online (Sandbox Code Playgroud)

在我的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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 10

您需要将其作为属性添加到元素:

    <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>
Run Code Online (Sandbox Code Playgroud)


小智 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> 
Run Code Online (Sandbox Code Playgroud)

其他方式是有一个内联地图(或通过fn:document()):

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

以此作为顶级元素

<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"/>
Run Code Online (Sandbox Code Playgroud)