xml 在 xslt 中显示变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12723393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display value of variable in xslt
提问by dazzle
Hi I want to populate the value of maxbars variable in the width in the percentage format, but for some reasons its not taking its value. Can you please help.
嗨,我想以百分比格式填充宽度中 maxbars 变量的值,但由于某些原因,它没有采用它的值。你能帮忙吗。
Example: I want to display it as width:10.9% format
示例:我想将其显示为 width:10.9% 格式
<xsl:for-each select="catalog/cd/price">
Current node:
<xsl:variable name="maxbars" select="."/>
<div style="width: 200px; height: 20px;">
<div style="width: maxbars%; height: 18px; background-color: red"></div>
</div>
<br/>
</xsl:for-each>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
回答by nd.
You must indicate that you are using the maxbarsvariable. If you are using it inside of an attribute, you can use XSL-T's curly brace syntax for xPath expressions:
您必须表明您正在使用该maxbars变量。如果在属性内部使用它,则可以对 xPath 表达式使用 XSL-T 的花括号语法:
<div style="width: {$maxbars}%; height: 18px; background-color: red"></div>
Important:The braces are placed aroundthe expression and you use the $inside of the braces.
重要提示:大括号位于表达式周围,您使用$大括号的内部。
If you want to insert variables (and other xPath expressions) outside of attributes, then you must use the <xsl:value-of>element:
如果要在属性之外插入变量(和其他 xPath 表达式),则必须使用该<xsl:value-of>元素:
<span>Price: <xsl:value-of select="$maxbars"/></span>
回答by StuartLC
Edit- nd's answer is more elegant - the {} technique is more concise.
编辑- nd 的回答更优雅 - {} 技术更简洁。
As an alternative, you can build up the divelement manually, in order to substitute the $maxbarsvariable.
作为替代方案,您可以div手动构建元素,以替换$maxbars变量。
<xsl:template match="/">
<xsl:for-each select="catalog/cd/price">
Current node:
<xsl:variable name="maxbars" select="."/>
<div style="width: 200px; height: 20px;">
<xsl:element name="div">
<xsl:attribute name="style">
<xsl:text>width: </xsl:text>
<xsl:value-of select="$maxbars" />
<xsl:text>%; height: 18px; background-color: red</xsl:text>
</xsl:attribute>
</xsl:element>
</div>
<br/>
</xsl:for-each>
</xsl:template>

