Xslt 当颜色在XML文档中时,如何向XSL中的Div添加颜色
我有一个XSL 1.0文档,其中包含以下内容:Xslt 当颜色在XML文档中时,如何向XSL中的Div添加颜色,xslt,Xslt,我有一个XSL 1.0文档,其中包含以下内容: <div class="workgroup_title"> <xsl:value-of select="./@name"/> </div> 我需要设置此元素的颜色。颜色在XML文件中 <abc.xyz.color>FF5733</abc.xyz.color> FF5733 为了得到它,我使用以下方法: <xsl:value-of select="./abc.xyz.co
<div class="workgroup_title">
<xsl:value-of select="./@name"/>
</div>
我需要设置此元素的颜色。颜色在XML文件中
<abc.xyz.color>FF5733</abc.xyz.color>
FF5733
为了得到它,我使用以下方法:
<xsl:value-of select="./abc.xyz.color"/>
我想做的是
<div class="workgroup_title" style="color:"#<xsl:value-of select="./abc.xyz.color"/>>
<xsl:value-of select="./@name"/>
</div>
但这是不允许的
或:
#
但是
color
不是可以这样设置的属性之一。您可以使用属性值模板来计算文本结果元素的(部分)值:…
您可以使用属性值模板来计算(部分)文本结果元素的值:..
以下模板应满足您的需要:
<xsl:template match="text()" /> <!-- Removes the text from the <abc.xyz.color>FF5733</abc.xyz.color> element -->
<xsl:template match="/*"> <!-- Copies the root element and its namespace -->
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[@class='workgroup_title']"> <!-- Applies the template to the <div> element -->
<xsl:copy>
<xsl:attribute name="style"><xsl:value-of select="concat('color: #',../abc.xyz.color,';')"/></xsl:attribute>
<xsl:copy-of select="node()|@*" />
</xsl:copy>
</xsl:template>
其输出为:
<root xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<div style="color: #FF5733;" class="workgroup_title">
<xsl:value-of select="./@name"/>
</div>
</root>
以下模板应满足您的需要:
<xsl:template match="text()" /> <!-- Removes the text from the <abc.xyz.color>FF5733</abc.xyz.color> element -->
<xsl:template match="/*"> <!-- Copies the root element and its namespace -->
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[@class='workgroup_title']"> <!-- Applies the template to the <div> element -->
<xsl:copy>
<xsl:attribute name="style"><xsl:value-of select="concat('color: #',../abc.xyz.color,';')"/></xsl:attribute>
<xsl:copy-of select="node()|@*" />
</xsl:copy>
</xsl:template>
其输出为:
<root xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<div style="color: #FF5733;" class="workgroup_title">
<xsl:value-of select="./@name"/>
</div>
</root>
太棒了!花括号?谁知道?!这是XSLT1.0吗?@dzmr83,当然,请看,XSLT1.0之后就可以使用了。太棒了!花括号?谁知道?!这是XSLT 1.0吗?@dzmr83,当然可以,请参阅,自XSLT 1开始提供。