在特定条件下向 `<xsl:element>` 添加 class 属性
add a class attribute to `<xsl:element>` under a certain condition
我想在特定条件下将 class 添加到 <xsl:element>
,我有一个 xml 标签,其属性名为 def,我需要添加一个 class 当这个属性存在时。
这是 xslt 元素:
<xsl:element name="{if (@id='123') then 'th' else 'td'} >
<xsl:apply-template />
</xsl:element>
我尝试过的:
<xsl:attribute name="class">def-
<xsl:if test="fn:exists(@def)">
<xsl:value-of select="@def"/>
</xsl:if>
</xsl:attribute>
<xsl:element name="{if (@role='rowhead' ) then 'th' else 'td'}" use-attribute-sets="{$class}" >
<xsl:apply-templates />
</xsl:element>
我想要一个匹配的表达式:
<th scope="row" class="def-{if (fn:exists(@def)) then @def else ''}">
<td scope="row" class="def-{if (fn:exists(@def)) then @def else ''}">
使用:
<xsl:element>
要根据特定条件动态创建属性,请将您的代码重新调整为...
<xsl:element name="{if (@role='rowhead' ) then 'th' else 'td'}">
<xsl:if test="fn:exists(@def)">
<xsl:attribute name="class" select="concat('def-', @def)"/>
</xsl:if>
<xsl:apply-templates />
</xsl:element>
如果您在 xsl:element
之后立即使用 xsl:attribute
,该属性将附加到元素上。请注意,必须在任何子元素之前创建属性。
我想在特定条件下将 class 添加到 <xsl:element>
,我有一个 xml 标签,其属性名为 def,我需要添加一个 class 当这个属性存在时。
这是 xslt 元素:
<xsl:element name="{if (@id='123') then 'th' else 'td'} >
<xsl:apply-template />
</xsl:element>
我尝试过的:
<xsl:attribute name="class">def-
<xsl:if test="fn:exists(@def)">
<xsl:value-of select="@def"/>
</xsl:if>
</xsl:attribute>
<xsl:element name="{if (@role='rowhead' ) then 'th' else 'td'}" use-attribute-sets="{$class}" >
<xsl:apply-templates />
</xsl:element>
我想要一个匹配的表达式:
<th scope="row" class="def-{if (fn:exists(@def)) then @def else ''}">
<td scope="row" class="def-{if (fn:exists(@def)) then @def else ''}">
使用:
<xsl:element>
要根据特定条件动态创建属性,请将您的代码重新调整为...
<xsl:element name="{if (@role='rowhead' ) then 'th' else 'td'}">
<xsl:if test="fn:exists(@def)">
<xsl:attribute name="class" select="concat('def-', @def)"/>
</xsl:if>
<xsl:apply-templates />
</xsl:element>
如果您在 xsl:element
之后立即使用 xsl:attribute
,该属性将附加到元素上。请注意,必须在任何子元素之前创建属性。