2

こんにちは、幅の maxbars 変数の値をパーセンテージ形式で入力したいのですが、何らかの理由でその値を取得していません。助けてください。

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

4

2 に答える 2

9

maxbars変数を使用していることを示す必要があります。属性内で使用している場合は、xPath式にXSL-Tの中括弧構文を使用できます。

<div style="width: {$maxbars}%; height: 18px; background-color: red"></div>

重要:中括弧は式の周囲に配置され$、中括弧の内側を使用します。

属性の外に変数(およびその他のxPath式)を挿入する場合は、次の<xsl:value-of>要素を使用する必要があります。

<span>Price: <xsl:value-of select="$maxbars"/></span>
于 2012-10-04T08:59:10.697 に答える
1

編集- ndの答えはよりエレガントです - {} テクニックはより簡潔です。

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>
于 2012-10-04T08:49:10.643 に答える